Cross Domain ASP.Net Web API Calling Using JQuery AJAX

If you have created a Restful API using the ASP.NET Web API and if your API is in one domain and the UI is in another domain then you might get errors due to cross-domain issues.

  1. OPTIONS http://localhost:5000/api/ 404 (Not Found).

  2. XMLHttpRequest cannot load http://localhost:5000/api/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:54317' is therefore not allowed access. The response had HTTP status code 404.

In other words you cannot make a call to the WebAPI via your front end that is hosted on a different domain.
So you must use JSONP to get data using AJAX, not by JSON.

To achieve this goal you need to install JsonpMediaTypeFormatter.

For installing the JsonpMediaTypeFormatter in Visual Studio, search JsonpMediaTypeFormatter in "Manage NuGet Packages" and install it.



Then you can see in your project references that a new DLL was added.

WebApiContrib.Formatting.Jsonp.dll

To use this DLL you need to change in the Application_Start() method in the Global.asax.cs file.

  1. using WebApiContrib.Formatting.Jsonp;    
  2. using System.Net.Http.Formatting;     
  1. GlobalConfiguration.Configuration.Formatters.Clear();   
  2. GlobalConfiguration.Configuration.Formatters.Add(new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));   
Or you can use JsonMediaTypeFormatter code, you need to write this code in the App_Start folder and register this code in Application_Start().

JsonMediaTypeFormatter code:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Net.Http.Formatting;    
  6. using System.Net.Http.Headers;    
  7. using System.Net.Http;    
  8. using Newtonsoft.Json.Converters;    
  9. using System.IO;    
  10. using System.Net;    
  11. using System.Threading.Tasks;    
  12.     
  13. namespace WebAPI    
  14. {    
  15.     /// <summary>    
  16.     /// Handles JsonP requests when requests are fired with text/javascript    
  17.     /// </summary>    
  18.     public class JsonpFormatter : JsonMediaTypeFormatter    
  19.     {    
  20.     
  21.         public JsonpFormatter()    
  22.         {    
  23.             SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));    
  24.             SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));    
  25.     
  26.             JsonpParameterName = "callback";    
  27.         }    
  28.     
  29.         /// <summary>    
  30.         ///  Name of the query string parameter to look for    
  31.         ///  the jsonp function name    
  32.         /// </summary>    
  33.         public string JsonpParameterName { getset; }    
  34.     
  35.         /// <summary>    
  36.         /// Captured name of the Jsonp function that the JSON call    
  37.         /// is wrapped in. Set in GetPerRequestFormatter Instance    
  38.         /// </summary>    
  39.         private string JsonpCallbackFunction;    
  40.     
  41.     
  42.         public override bool CanWriteType(Type type)    
  43.         {    
  44.             return true;    
  45.         }    
  46.     
  47.         /// <summary>    
  48.         /// Override this method to capture the Request object    
  49.         /// </summary>    
  50.         /// <param name="type"></param>    
  51.         /// <param name="request"></param>    
  52.         /// <param name="mediaType"></param>    
  53.         /// <returns></returns>    
  54.         public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)    
  55.         {    
  56.             var formatter = new JsonpFormatter()    
  57.             {    
  58.                 JsonpCallbackFunction = GetJsonCallbackFunction(request)    
  59.             };    
  60.     
  61.             // this doesn't work unfortunately    
  62.             //formatter.SerializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;    
  63.     
  64.             // You have to reapply any JSON.NET default serializer Customizations here        
  65.             formatter.SerializerSettings.Converters.Add(new StringEnumConverter());    
  66.             formatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;    
  67.     
  68.             return formatter;    
  69.         }    
  70.     
  71.     
  72.         public override Task WriteToStreamAsync(Type type, object value,    
  73.                                         Stream stream,    
  74.                                         HttpContent content,    
  75.                                         TransportContext transportContext)    
  76.         {    
  77.             if (string.IsNullOrEmpty(JsonpCallbackFunction))    
  78.                 return base.WriteToStreamAsync(type, value, stream, content, transportContext);    
  79.     
  80.             StreamWriter writer = null;    
  81.     
  82.             // write the pre-amble    
  83.             try    
  84.             {    
  85.                 writer = new StreamWriter(stream);    
  86.                 writer.Write(JsonpCallbackFunction + "(");    
  87.                 writer.Flush();    
  88.             }    
  89.             catch (Exception ex)    
  90.             {    
  91.                 try    
  92.                 {    
  93.                     if (writer != null)    
  94.                         writer.Dispose();    
  95.                 }    
  96.                 catch { }    
  97.     
  98.                 var tcs = new TaskCompletionSource<object>();    
  99.                 tcs.SetException(ex);    
  100.                 return tcs.Task;    
  101.             }    
  102.     
  103.             return base.WriteToStreamAsync(type, value, stream, content, transportContext)    
  104.                        .ContinueWith(innerTask =>    
  105.                        {    
  106.                            if (innerTask.Status == TaskStatus.RanToCompletion)    
  107.                            {    
  108.                                writer.Write(")");    
  109.                                writer.Flush();    
  110.                            }    
  111.                            writer.Dispose();    
  112.                            return innerTask;    
  113.                        }, TaskContinuationOptions.ExecuteSynchronously)    
  114.                             .Unwrap();    
  115.         }    
  116.     
  117.         /// <summary>    
  118.         /// Retrieves the Jsonp Callback function    
  119.         /// from the query string    
  120.         /// </summary>    
  121.         /// <returns></returns>    
  122.         private string GetJsonCallbackFunction(HttpRequestMessage request)    
  123.         {    
  124.             if (request.Method != HttpMethod.Get)    
  125.                 return null;    
  126.     
  127.             var query = HttpUtility.ParseQueryString(request.RequestUri.Query);    
  128.             var queryVal = query[this.JsonpParameterName];    
  129.     
  130.             if (string.IsNullOrEmpty(queryVal))    
  131.                 return null;    
  132.     
  133.             return queryVal;    
  134.         }    
  135.     }    
  136. }  
For using this code you need to make the following change in the Application_Start() method in the Global.asax.cs file.
  1. GlobalConfiguration.Configuration.Formatters.Insert(0, new WebAPI.JsonpFormatter());   
jQuery code to use the Web API:
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    
  2. <script language="javascript" type="text/javascript">    
  3.     function GetInformation() {    
  4.         var apiServicePath = "http://localhost:5000/api/";    
  5.         jQuery.ajax({    
  6.             crossDomain: true,    
  7.             dataType: "jsonp",    
  8.             url: apiServicePath + "test/GetInformation",    
  9.             async: false,    
  10.             context: document.body    
  11.         }).done(function (data) {    
  12.             alert("Done");    
  13.         });    
  14.     };    
  15. </script>  
Thanks.


Similar Articles