Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
- Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource.
- public class PreflightRequestsHandler : DelegatingHandler
- {
- protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- if (request.Headers.Contains("Origin") && request.Method.Method.Equals("OPTIONS"))
- {
- var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
- // Define and add values to variables: origins, headers, methods (can be global)
- response.Headers.Add("Access-Control-Allow-Origin", origins);
- response.Headers.Add("Access-Control-Allow-Headers", headers);
- response.Headers.Add("Access-Control-Allow-Methods", methods);
- var tsc = new TaskCompletionSource<HttpResponseMessage>();
- tsc.SetResult(response);
- return tsc.Task;
- }
- return base.SendAsync(request, cancellationToken);
- }
- }
- public static void Register(HttpConfiguration config)
- {
- // Define and add values to variables: origins, headers, methods (can be global)
- // Enable global CORS
- config.EnableCors(new EnableCorsAttribute(origins, headers, methods));
- // Add handler to deal with preflight requests, this is the important part
- config.MessageHandlers.Add(new PreflightRequestsHandler()); // Defined above
- .
- .
- .
- }
- The browser can skip the preflight request if the following conditions are true:
- The request method is GET, HEAD, or POST, **and**
- The application does not set any request headers other than Accept, Accept-Language, Content-Language, Content-Type, or Last-Event-ID, **and**
- The Content-Type header (if set) is one of the following:
- - application/x-www-form-urlencoded
- - multipart/form-data
- - text/plain
- <system.webServer>
- <handlers>
- <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
- <remove name="OPTIONSVerbHandler" />
- <remove name="TRACEVerbHandler" />
- <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
- </handlers>
- </system.webServer>
Add Comment
Please, Sign In to add comment