When 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.
- http://localhost:5000/api/ 404 (Not Found).
- 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.
Then you can resolve it using Web API Cross Handler, you need to add a WepAPICrossHandler.cs code in the App_Start folder and register this code in Application_Start().
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Net.Http;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Net;
- namespace WebAPI
- {
- public class WepAPICrossHandler : DelegatingHandler
- {
- const string Origin = "Origin";
- const string AccessControlRequestMethod = "Access-Control-Request-Method";
- const string AccessControlRequestHeaders = "Access-Control-Request-Headers";
- const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
- const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
- const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";
- protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- bool isCorsRequest = request.Headers.Contains(Origin);
- bool isPreflightRequest = request.Method == HttpMethod.Options;
- if (isCorsRequest)
- {
- if (isPreflightRequest)
- {
- HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
- response.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
- string accessControlRequestMethod = request.Headers.GetValues(AccessControlRequestMethod).FirstOrDefault();
- if (accessControlRequestMethod != null)
- {
- response.Headers.Add(AccessControlAllowMethods, accessControlRequestMethod);
- }
- string requestedHeaders = string.Join(", ", request.Headers.GetValues(AccessControlRequestHeaders));
- if (!string.IsNullOrEmpty(requestedHeaders))
- {
- response.Headers.Add(AccessControlAllowHeaders, requestedHeaders);
- }
- TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
- tcs.SetResult(response);
- return tcs.Task;
- }
- else
- {
- return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
- {
- HttpResponseMessage resp = t.Result;
- resp.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
- return resp;
- });
- }
- }
- else
- {
- return base.SendAsync(request, cancellationToken);
- }
- }
- }
- }
- GlobalConfiguration.Configuration.MessageHandlers.Add(new WepAPICrossHandler());
Arun UniyalPosted Dec 15, 2020, 5:03 PM
Post method throw Cors issue and also both methods (GET and POST) not working when we r passing headers in ajax call. please provide solution for same.
Midhun TpPosted Mar 10, 2017, 1:25 AM
This was a real life saver. Thanks for the share buddy :)
Jagdev MishraPosted May 10, 2016, 1:42 AM
Try this............[httpPost]public string PostSubmitdata(Employee emp) { return emp.EmployeeName; }
Sunil PandeyPosted Apr 21, 2016, 7:24 AM
not working in my case, I have tried to implement post request with parameter but its showing bad request error, without parameter its working i.e. h>>>> public string PostSubmitdata([FromBody]Employee emp) { return emp.EmployeeName; } not working :(
Vishal JadhavPosted Jan 21, 2015, 11:37 PM
Critical topic, but handled nicely. Keep it up..