简体   繁体   中英

VB.NET Web API CORS PUT Preflight 405 Error

I am using WEB API with CORS enabled and my GET & POSTs are working perfectly. However my PUTs aren't working thru CORS. I can use the 'Advanced REST' extension in Chrome and that works fine. I have various settings in the web config included

UPDATE: i wanted to note that my POST are working thru CORS. Which, if i'm understanding the process sends an OPTION verb first. That OPTION verb goes thru, however with PUT it doesn't.

UPDATE 2: So it appears a preflight is needed for PUTs (OPTIONS) verb. And i'm thinking i dont have that, and I can't really find how to make it.

<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" />
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>

and

$.ajax({
    type: "PUT",
    dataType: "json",
    url: url,
    contentType: "application/json",
    data: newMod,
    success: function (data) {
        OutfitModelKey = data;
        console.log(data);
        console.log(OutfitModelKey);
        if (callback) {
            callback();
        }
    },
    error: function (error) {
        alert("hi, error");
        //jsonValue = jQuery.parseJSON(error.responseText);
        //console.log(jsonValue);
        //ToastDanger("uh o!", jsonValue);
        //jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
    }
});

and

' PUT api/OutfitModel/5
Public Function PutOutfitModel(ByVal id As Integer, <FromBody()> ByVal value As PhotoModelManagerCL.objOutfitModel) As Boolean
    value.OutfitModelKey = id
    Dim success As Boolean = PhotoModelManagerCL.UpdateobjOutfitModelByKey(value)
    Return success
End Function

Chrome中的高级REST扩展功能可显示PUT调用正常运行

Chrome控制台显示PUT / OPTION失败。下班后。

It looks like specifying the methods explicitly fixes the problem

 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
 </httpProtocol>

In the case of the PUT verb, there's also a pre-request (some call it preflight): Options .

To handle it you need to send an empty response back. You can do this inside your actions, or you an do it globally like this:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    If Request.Headers.AllKeys.Contains("Origin") And Request.HttpMethod = "OPTIONS" Then
        Response.Flush()
    End If
End Sub

C# equivalent

protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        Response.Flush();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM