2013-04-10 55 views
1

我們正在開發ASP.NET MVC應用程序並希望使用Web API來處理業務功能。此外,我們還與Dyanmics AX和Dynamics CRM產品集成。因此,在本質上我想發展以下組件:構建具有RESTul服務的MVC應用程序

  1. 要實現這樣的(通過SOAP)與AX通信 RESTful服務,返回的數據傳輸對象。
  2. 建立一個RESTFul服務, 與CRM通信(再次通過SOAP),並返回一個數據傳輸對象 。

現在的問題是,例如將數據加載到一個訂單屏幕(這需要從AX和CRM數據),

a. Use the controller to make calls to the RESTFUL Services and then use a Model object to pass the data to the screen. 
(or) 
b. Do nothing in the controller, but use AJAX calls from the Razor to get data from the RESTFul services and load data into the screen. 

將不勝感激,如果任何人都可以扔在最佳實踐的一些光采用(關於使用MVC & RESTFUL服務構建這種場景)?

謝謝。

+0

你有什麼樣的代碼嘗試過嗎? – 2013-04-10 17:38:54

回答

1

如果您要調用外部RESTful服務,則需要通過POST或GET創建HTTP請求,並適當處理請求返回的響應。如果返回的數據是XML,則需要使用XML文檔解析器來使用XML。如果返回的數據是JSON,則可以使用如下所示的動態分析:Deserialize JSON into C# dynamic object? 這是處理JSON的一種非常好的方式,因爲它提供了一種類似於如何訪問JavaScript中的JSON對象的機制。

下面是一些代碼,我用我的Web API使用:

public class Base 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string UserAgent { get; set; } 
    public string ContentType { get; set; } 
    public CookieCollection Cookies { get; set; } 
    public CookieContainer Container { get; set; } 

    public Base() 
    { 
     UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0"; 
     ContentType = "application/x-www-form-urlencoded"; 
     Cookies = new CookieCollection(); 
     Container = new CookieContainer(); 
    } 

    public Base(string username, string password) 
    { 
     Username = username; 
     Password = password; 
     UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0"; 
     ContentType = "application/x-www-form-urlencoded"; 
     Cookies = new CookieCollection(); 
     Container = new CookieContainer(); 
    } 

    public string Load(string uri, string postData = "", NetworkCredential creds = null, int timeout = 60000, string host = "", string referer = "", string requestedwith = "") 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.CookieContainer = Container; 
     request.CookieContainer.Add(Cookies);   
     request.UserAgent = UserAgent; 
     request.AllowWriteStreamBuffering = true; 
     request.ProtocolVersion = HttpVersion.Version11; 
     request.AllowAutoRedirect = true; 
     request.ContentType = ContentType; 
     request.PreAuthenticate = true; 

     if (requestedwith.Length > 0) 
      request.Headers["X-Requested-With"] = requestedwith; // used for simulating AJAX requests 

     if (host.Length > 0) 
      request.Host = host; 

     if (referer.Length > 0) 
      request.Referer = referer; 

     if (timeout > 0) 
      request.Timeout = timeout; 

     if (creds != null) 
      request.Credentials = creds; 

     if (postData.Length > 0) 
     { 
      request.Method = "POST"; 
      ASCIIEncoding encoding = new ASCIIEncoding(); 
      byte[] data = encoding.GetBytes(postData); 
      request.ContentLength = data.Length; 
      Stream newStream = request.GetRequestStream(); //open connection 
      newStream.Write(data, 0, data.Length); // Send the data. 
      newStream.Close(); 
     } 
     else 
      request.Method = "GET"; 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     Cookies = response.Cookies; 
     StringBuilder page; 
     using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
     { 
      page = new StringBuilder(sr.ReadToEnd()); 
      page = page.Replace("\r\n", ""); // strip all new lines and tabs 
      page = page.Replace("\r", ""); // strip all new lines and tabs 
      page = page.Replace("\n", ""); // strip all new lines and tabs 
      page = page.Replace("\t", ""); // strip all new lines and tabs 
     } 

     string str = page.ToString(); 
     str = Regex.Replace(str, @">\s+<", "><"); // remove all space in between tags 

     return str; 
    } 
} 

我工作的一個API,用於與Xbox Live的,PSN和蒸汽數據交互。每個服務都有自己的結構,這是我爲每個服務使用的基類。但是,我不打算詳細討論如何從這些服務中獲取數據。

+0

謝謝並確認我的理解..上述代碼可以用來從控制器進行HTTP調用WEB API服務..我認爲是正確的,並沒有違反MVC的任何原則。再次檢查它是否正確? – user1421803 2013-04-10 19:12:41

+1

這是調用外部服務時的一個好方法。此方法不違反MVC。您可以根據需要爲控制器添加儘可能多的幫助類。我在我正在開發的RESTful API上使用此代碼與幾個服務。 – 2013-04-10 19:24:21

+1

太好了。感謝和欣賞你的時間。 – user1421803 2013-04-10 19:29:04

相關問題