1

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

  1. 构建与 AX(通过 SOAP)通信并返回数据传输对象的 RESTFul 服务。
  2. 构建与 CRM 通信(再次通过 SOAP)并返回数据传输对象的 RESTFul 服务。

现在我的问题是,例如将数据加载到订单屏幕(需要来自 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 服务进行架构设计)有所了解,我将不胜感激?

4

1 回答 1

1

如果您正在调用外部 RESTful 服务,则需要通过 POST 或 GET 创建 HTTP 请求并适当地处理请求返回的响应。如果返回的数据是 XML,您将需要使用 XML 文档解析器来使用 XML。如果返回的数据是 JSON,您可以使用如下所示的动态解析:将JSON 反序列化为 C# 动态对象? 这是处理 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。每个服务都有自己的结构,这是我用于每个服务的基类。但是,我不打算在这里详细介绍如何从这些服务中获取数据。

于 2013-04-10T17:50:19.177 回答