我对我的 Email
Web API 2 Controller 执行了一个操作:
[Authorize]
[RoutePrefix("api/Email")]
public class EmailController : ApiController {
//...
[HttpDelete]
[Route("Remove/{id}")]
private void Remove(int id) {
_repo.Remove(id);
}
}
当我使用 DELETE
http://localhost:35191/api/Email/Remove/35571
(或任何其他方法)从 Fiddler 调用操作时,我得到500
返回通用 IIS 错误页面,没有提供有关错误的信息。
似乎错误发生在我的操作被调用之前,因为在操作中设置断点会导致断点永远不会被击中。
是否需要某种配置才能在 IIS (Express) 中使用 DELETE
方法?
我已经尝试在我的 web.config 中明确允许 DELETE
:
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
但无济于事。
最佳答案
您必须将公开的方法设为public
:
[HttpDelete]
[Route("Remove/{id}")]
public void Remove(int id) {
_repo.Remove(id);
}
如果这不起作用,那么您可能需要删除 WebDav(web.config):
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
</system.webServer>
关于c# - Web API 2 DELETE 方法总是返回 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220831/