简体   繁体   中英

500 Internal server error at PUT Web API

I have MVC Web API that have POST and PUT functions; POST function calls succeeded but PUT function call failed with:

internal server error;

Functions are identical "I use one function at a time and the other one will be commented; Just for testing purposes".

public HttpResponseMessage Put(string id)
{
   HttpStatusCode statusCode = HttpStatusCode.OK;
   return Request.CreateResponse<string>(statusCode, id);
}

public HttpResponseMessage Post(string id)
{
   HttpStatusCode statusCode = HttpStatusCode.OK;
   return Request.CreateResponse<string>(statusCode, id);
}

Edit: It works fine locally at my machine for both POST and PUT (Windows 8.1); but when i move it to another machine (Windows Server 2012 )only POST functions works.

Use POST to create resources when you do not know the resource identifier. With POST creates, it is best practice to return the status of “201 Created” and the location of the newly created resource, since its location was unknown at the time of submission. This allows the client to access the new resource later if they need to

Finally i found a solution for this issue, it seems that there is an issue in WebDav, in some cases it is not enough to remove it from your application Web.Config you should disable it from IIS by following steps from this article How to disable WEBDAV in IIS

I will update this answer when i found exactly what is the problem with WebDav that make removing it from application Web.Config not enough for windows 2012 but working fine in windows 8.1

I had the same problem. PUT and DELETE endpoints worked while I was debugging in visual studio, but not when I deployed to IIS.

I had already added this

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
      <remove name="WebDAVModule" />
    </modules>  
  </system.webServer>

in my web.config, so I wasn't thinking about the WebDav. Ebraheem's answer had me looking closer at the WebDav.

Ended up that the IIS Server had WebDav Publishing enabled in Features and Roles. So I removed it and now everything works as expected.

The remove <remove name="WebDAVModule" /> is not enough. What I found out is that you have to specifically remove it from handlers as well and also just to make sure the verbs are allowed you can set them in the security node. The following is what I set in my web.config which allowed the put and delete to work without setting anything in IIS.

<!-- After the <system.web> node -->
<system.webServer>
<handlers>
  <!-- default handler settings here if any -->
  <!-- Add the following to remove WebDAV handler -->
  <remove name="WebDAV" />
</handlers>

<modules runAllManagedModulesForAllRequests="false">
  <!-- Add the following to remove WebDAV module -->
  <remove name="WebDAVModule" />
</modules>

<validation validateIntegratedModeConfiguration="false" />

<security>
<!-- Add the following to specifically allow the GET,POST,DELETE and PUT verbs -->
  <requestFiltering>
    <verbs allowUnlisted="false">
      <add verb="GET" allowed="true" />
      <add verb="POST" allowed="true" />
      <add verb="DELETE" allowed="true" />
      <add verb="PUT" allowed="true" />
    </verbs>
  </requestFiltering>
</security>
</system.webServer>

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