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.
- OPTIONS http://localhost:5000/api/ 404 (Not Found).
- 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.
- using WebApiContrib.Formatting.Jsonp;
- using System.Net.Http.Formatting;
- GlobalConfiguration.Configuration.Formatters.Clear();
- 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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Net.Http.Formatting;
- using System.Net.Http.Headers;
- using System.Net.Http;
- using Newtonsoft.Json.Converters;
- using System.IO;
- using System.Net;
- using System.Threading.Tasks;
-
- namespace WebAPI
- {
-
-
-
- public class JsonpFormatter : JsonMediaTypeFormatter
- {
-
- public JsonpFormatter()
- {
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
-
- JsonpParameterName = "callback";
- }
-
-
-
-
-
- public string JsonpParameterName { get; set; }
-
-
-
-
-
- private string JsonpCallbackFunction;
-
-
- public override bool CanWriteType(Type type)
- {
- return true;
- }
-
-
-
-
-
-
-
-
- public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)
- {
- var formatter = new JsonpFormatter()
- {
- JsonpCallbackFunction = GetJsonCallbackFunction(request)
- };
-
-
-
-
-
- formatter.SerializerSettings.Converters.Add(new StringEnumConverter());
- formatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
-
- return formatter;
- }
-
-
- public override Task WriteToStreamAsync(Type type, object value,
- Stream stream,
- HttpContent content,
- TransportContext transportContext)
- {
- if (string.IsNullOrEmpty(JsonpCallbackFunction))
- return base.WriteToStreamAsync(type, value, stream, content, transportContext);
-
- StreamWriter writer = null;
-
-
- try
- {
- writer = new StreamWriter(stream);
- writer.Write(JsonpCallbackFunction + "(");
- writer.Flush();
- }
- catch (Exception ex)
- {
- try
- {
- if (writer != null)
- writer.Dispose();
- }
- catch { }
-
- var tcs = new TaskCompletionSource<object>();
- tcs.SetException(ex);
- return tcs.Task;
- }
-
- return base.WriteToStreamAsync(type, value, stream, content, transportContext)
- .ContinueWith(innerTask =>
- {
- if (innerTask.Status == TaskStatus.RanToCompletion)
- {
- writer.Write(")");
- writer.Flush();
- }
- writer.Dispose();
- return innerTask;
- }, TaskContinuationOptions.ExecuteSynchronously)
- .Unwrap();
- }
-
-
-
-
-
-
- private string GetJsonCallbackFunction(HttpRequestMessage request)
- {
- if (request.Method != HttpMethod.Get)
- return null;
-
- var query = HttpUtility.ParseQueryString(request.RequestUri.Query);
- var queryVal = query[this.JsonpParameterName];
-
- if (string.IsNullOrEmpty(queryVal))
- return null;
-
- return queryVal;
- }
- }
- }
For using this code you need to make the following change in the Application_Start() method in the Global.asax.cs file.
- GlobalConfiguration.Configuration.Formatters.Insert(0, new WebAPI.JsonpFormatter());
jQuery code to use the Web API:
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- function GetInformation() {
- var apiServicePath = "http://localhost:5000/api/";
- jQuery.ajax({
- crossDomain: true,
- dataType: "jsonp",
- url: apiServicePath + "test/GetInformation",
- async: false,
- context: document.body
- }).done(function (data) {
- alert("Done");
- });
- };
- </script>
Thanks.