简体   繁体   English

PUT Web API 出现 500 内部服务器错误

[英]500 Internal server error at PUT Web API

I have MVC Web API that have POST and PUT functions;我有具有 POST 和 PUT 功能的 MVC Web API; POST function calls succeeded but PUT function call failed with: POST 函数调用成功但 PUT 函数调用失败:

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);编辑:对于 POST 和 PUT(Windows 8.1),它在我的机器上本地运行良好; but when i move it to another machine (Windows Server 2012 )only POST functions works.但是当我将它移动到另一台机器(Windows Server 2012)时,只有 POST 功能有效。

Use POST to create resources when you do not know the resource identifier.当您不知道资源标识符时,使用 POST 创建资源。 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.对于 POST 创建,最佳做法是返回“201 Created”状态和新创建资源的位置,因为在提交时它的位置是未知的。 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最后我找到了这个问题的解决方案,似乎 WebDav 中存在问题,在某些情况下,将它从您的应用程序 Web.Config 中删除是不够的,您应该按照本文中的步骤从 IIS 中禁用它如何禁用IIS 中的 WEBDAV

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当我发现 WebDav 究竟是什么问题导致从应用程序 Web.Config 中删除它对于 windows 2012 来说不够但在 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. PUT 和 DELETE 端点在我在 visual studio 中调试时有效,但在我部署到 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.在我的 web.config 中,所以我没有考虑 WebDav。 Ebraheem's answer had me looking closer at the WebDav. Ebraheem 的回答让我仔细研究了 WebDav。

Ended up that the IIS Server had WebDav Publishing enabled in Features and Roles.最后,IIS 服务器在功能和角色中启用了 WebDav 发布。 So I removed it and now everything works as expected.所以我删除了它,现在一切都按预期工作了。

The remove <remove name="WebDAVModule" /> is not enough.删除<remove name="WebDAVModule" />是不够的。 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.以下是我在我的 web.config 中设置的,它允许放置和删除工作而无需在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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