Guest User

Untitled

a guest
Feb 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
  2. Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  3.  
  4. public class PreflightRequestsHandler : DelegatingHandler
  5. {
  6. protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  7. {
  8. if (request.Headers.Contains("Origin") && request.Method.Method.Equals("OPTIONS"))
  9. {
  10. var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
  11. // Define and add values to variables: origins, headers, methods (can be global)
  12. response.Headers.Add("Access-Control-Allow-Origin", origins);
  13. response.Headers.Add("Access-Control-Allow-Headers", headers);
  14. response.Headers.Add("Access-Control-Allow-Methods", methods);
  15. var tsc = new TaskCompletionSource<HttpResponseMessage>();
  16. tsc.SetResult(response);
  17. return tsc.Task;
  18. }
  19. return base.SendAsync(request, cancellationToken);
  20. }
  21.  
  22. }
  23.  
  24. public static void Register(HttpConfiguration config)
  25. {
  26. // Define and add values to variables: origins, headers, methods (can be global)
  27. // Enable global CORS
  28. config.EnableCors(new EnableCorsAttribute(origins, headers, methods));
  29.  
  30. // Add handler to deal with preflight requests, this is the important part
  31. config.MessageHandlers.Add(new PreflightRequestsHandler()); // Defined above
  32. .
  33. .
  34. .
  35. }
  36.  
  37. The browser can skip the preflight request if the following conditions are true:
  38.  
  39. The request method is GET, HEAD, or POST, **and**
  40. The application does not set any request headers other than Accept, Accept-Language, Content-Language, Content-Type, or Last-Event-ID, **and**
  41. The Content-Type header (if set) is one of the following:
  42. - application/x-www-form-urlencoded
  43. - multipart/form-data
  44. - text/plain
  45.  
  46. <system.webServer>
  47. <handlers>
  48. <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  49. <remove name="OPTIONSVerbHandler" />
  50. <remove name="TRACEVerbHandler" />
  51. <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  52. </handlers>
  53. </system.webServer>
Add Comment
Please, Sign In to add comment