1

We are developing a ASP.NET MVC application and want to use Web API to handle the business functionality. In addition we also have integration with Dynamics AX and Dynamics CRM products. So, in essence I was thinking to develop the following components :

  1. To build a RESTFul Service that communicates with AX (through SOAP) and return a Data Transfer Object.
  2. To build a RESTFul Service that communicates with CRM (again through SOAP) and return a Data Transfer Object.

Now my question is, for instance to load data into an Order Screen (which need data from AX and 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.

Would appreciate, If anyone could throw some light on the best practice to adopt (with respect to architecting in such a scenario using MVC & RESTFUL services)?

user1421803
  • 31
  • 1
  • 2

1 Answers1

1

If you are calling external RESTful services, you will need to create HTTP requests via POST or GET and handle the response returned by the requests appropriately. If the data returned is XML, you'll need to consume the XML using an XML document parser. If the data returned is JSON, you could use dynamic parsing like shown here: Deserialize JSON into C# dynamic object? This is a very nice way to deal with JSON as it provides a mechanism similar how you would access a JSON object in JavaScript.

Here is some code that I use with my web APIs:

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;
    }
}

I'm working on an API for interacting with Xbox Live, PSN, and Steam data. Each service has their own structure and this is the base class that I use for each service. However, I'm not going to go into specifics on how to get data from each of those services here.

Community
  • 1
  • 1
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • Thanks and to confirm my understanding.. The above code can be used from the controller to make HTTP calls to the WEB API Services.. which I guess is alright and doesn't violate any principles of MVC. Again just checking if its the right approach? – user1421803 Apr 10 '13 at 19:12
  • 1
    This is a good approach when calling external services. This method doesn't violate MVC. You can add as many helper classes for your controller as you need. I use this code with several services on my RESTful API that I'm developing. – Cameron Tinker Apr 10 '13 at 19:24