405 method options not allowed in asp.net web api controller?

前端 未结 1 670
小蘑菇
小蘑菇 2020-12-19 04:24

I have this very common issue where it says method not allowed (OPTIONS) for a GET request. I am getting the following error whenever I make an API

相关标签:
1条回答
  • 2020-12-19 04:49

    In your handlers after <remove name="OPTIONSVerbHandler"/>, add this:

    <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS"
      modules="IsapiModule" requireAccess="None"
      scriptProcessor="C:\Windows\System32\inetsrv\asp.dll"
      resourceType="Unspecified" />
    

    See the answer at IIS hijacks CORS Preflight OPTIONS request.

    Or maybe even just this:

     <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS"
       modules="ProtocolSupportModule" requireAccess="None" />
    

    If that doesn’t work, something that will is adding the following in your global.asax or other code:

    protected void Application_BeginRequest(object sender,EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000" );
            HttpContext.Current.Response.End();
        }
    }
    
    0 讨论(0)
提交回复
热议问题