Web API - 405 - 请求的资源不支持 http 方法“PUT"
我有一个 Web API 项目,但我无法针对它启用PUT/Patch"请求.
I have a Web API project and I am unable to enable "PUT/Patch" requests against it.
我从 fiddler 得到的回应是:
The response I get from fiddler is:
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET,POST,DELETE
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcZG90TmV0XFdlYkFQSVxBZFNlcnZpY2VcQWRTZXJ2aWNlXGFwaVxpbXByZXNzaW9uXDE1?=
X-Powered-By: ASP.NET
Date: Tue, 06 May 2014 14:10:35 GMT
Content-Length: 72
{"message":"The requested resource does not support http method 'PUT'."}
根据以上回复,不接受PUT"动词.但是,我无法弄清楚相关处理程序的配置位置.
Based on the above response, "PUT" verbs are not accepted. However, I'm unable to figure out where the related handler is configured.
类的Put"方法声明如下:
The "Put" method of class is declared as follows:
[HttpPatch]
[HttpPut]
public HttpResponseMessage Put(Int32 aID, [FromBody] ImpressionModel impressionModel)
{
bla, bla, bla, bla
}
我已阅读并实施了以下线程中解释的更改:- Asp.NET Web API - 405 - 不允许用于访问此页面的 HTTP 动词 - 如何设置处理程序映射- http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications
I have read and implemented the changes explained in the following threads: - Asp.NET Web API - 405 - HTTP verb used to access this page is not allowed - how to set handler mappings - http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications
没有任何效果,因为我在尝试针对我的 Web API 项目发出PUT"命令时仍然收到 405 响应.
Nothing has worked as I'm still getting a 405 response when trying to issue a "PUT" command against my Web API project.
我什至注释掉了 ApplicationsHost.config 文件中的所有处理程序".
I even commented out all of the "Handlers" in the ApplicationsHost.config file.
使用 VS2012 Premium 和 IIS Express(我假设它是第 8 版).我也尝试过 VS Dev Server,但结果也一样.
Working with VS2012 Premium and IIS Express (I'm assuming it's version 8). I also tried the VS Dev Server but that gave me the same result also.
我没有想法了.任何帮助将不胜感激.
I'm out of ideas. Any help would be appreciated.
谢谢李
你在使用属性路由吗?
这个神秘的错误是一个路由属性问题.这在您的 WebAPIConfig 中启用为:
This mystic error was a route attributes issue. This is enabled in your WebAPIConfig as:
config.MapHttpAttributeRoutes();
事实证明,Web Api 控制器无法承载基于动词的动作方法和传统动作名称路由的混合体.";https://aspnetwebstack.codeplex.com/workitem/184
It turns out Web Api Controllers "cannot host a mixture of verb-based action methods and traditional action name routing. "; https://aspnetwebstack.codeplex.com/workitem/184
简而言之:我需要在我的 API 控制器中使用 [Route] 属性标记我的所有操作,否则在尝试通过传统路由定位时该操作是隐藏的"(405'd).
API 控制器:
[RoutePrefix("api/quotes")]
public class QuotesController : ApiController
{
...
// POST api/Quote
[ResponseType(typeof(Quote))]
[Route]
public IHttpActionResult PostQuote(Quote quote)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Quotes.Add(quote);
db.SaveChanges();
return CreatedAtRoute("", new { id = quote.QuoteId }, quote);
}
注意:我的路由未命名,因此 CreatedAtRoute() 名称只是一个空字符串.
WebApiConfig.cs:
WebApiConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
希望能帮到你