1

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.

Ebraheem
  • 603
  • 6
  • 24
  • Possible duplicate of [Enable Http PUT requests on .NET MVC](http://stackoverflow.com/questions/9161354/enable-http-put-requests-on-net-mvc) – Knelis Jan 12 '16 at 08:26
  • i already configured this line in Web.config '' and as I mentioned it is working fine in my machine and same Web.config configuration are applied for both machines – Ebraheem Jan 12 '16 at 08:38
  • Please post the complete exception details. – Knelis Jan 12 '16 at 08:45
  • I only have exception information in the response in my client, i think exception is happened before reach PUT function, maybe something in routing; I'm new to MVC, is there a way to know what exactly happened? I read some articles about troubleshooting exceptions but nothing was helpful. – Ebraheem Jan 12 '16 at 09:15
  • You should be able to configure the server to return more detailed errors, not just 'Internal Server Error', because that doesn't help you to figure out what the problem is. Try setting `` to see if you get more details. – Knelis Jan 12 '16 at 09:19
  • Thank you for your reply, i will try to get more information about exception and update my question. – Ebraheem Jan 12 '16 at 09:45

4 Answers4

0

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

  • Thank you for your reply, Above code is just for testing; actually what i need to do is to update existing records in database this is why I'm using PUT, but when my calls failed i try a simpler code just to check if problem is in my code or in the environment (maybe configuration); and finally i notice that all PUT calls failed. – Ebraheem Jan 12 '16 at 09:18
0

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

Ebraheem
  • 603
  • 6
  • 24
0

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.

0

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Nasir T
  • 2,603
  • 1
  • 9
  • 15