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

前端 未结 1 681
小蘑菇
小蘑菇 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 , add this:

    
    

    See the answer at IIS hijacks CORS Preflight OPTIONS request.

    Or maybe even just this:

     
    

    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)
提交回复
热议问题