使用RESTful Services构建MVC应用程序

时间:2013-04-10 16:19:04

标签: asp.net-mvc rest

我们正在开发ASP.NET MVC应用程序,并希望使用Web API来处理业务功能。此外,我们还与Dyanmics AX和Dynamics CRM产品集成。所以,从本质上讲,我正在考虑开发以下组件:

  1. 构建与AX通信的RESTFul服务(通过SOAP) 并返回一个数据传输对象。
  2. 构建一个RESTFul服务 与CRM通信(再次通过SOAP)并返回数据 转移对象。
  3. 现在我的问题是,例如将数据加载到订单屏幕(需要来自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服务的情况下进行架构设计)?

    感谢。

1 个答案:

答案 0 :(得分: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;
    }
}

我正在开发一个用于与Xbox Live,PSN和Steam数据进行交互的API。每个服务都有自己的结构,这是我用于每个服务的基类。但是,我不打算详细介绍如何从这些服务中获取数据。