问题描述
我们有我试图更新包含的WebAPI的MVC项目。为了得到我们使用AttributeRouting所需路由。所有来电似乎除了[PUT]正确的路由,它返回一个405我已经简化控制器和行动,仍然收到错误用[PUT]除非我有[HttpPut]也。不知道我错过了什么。
We have an MVC project that I am attempting to update to include WebApi. In order to get the required routes we are using AttributeRouting. All the calls seem to be routing correctly except for [PUT] which returns a 405. I have simplified the controller and actions and still receive the error with the [PUT] unless I include [HttpPut] also. Not sure what I am missing.
[RoutePrefix("api/Sites")]
public class SitesController : BaseApiController
{
[POST("")]
public bool CreateSite(SiteSignupArgs args)
{
...
}
[GET("Statuses")]
public IList<SiteAuditViewModel> GetStatuses()
{
...
}
[PUT("Statuses/{siteId}")]
[HttpPut] // This is required or 405 is returned
public HttpResponseMessage UpdateStatus(string siteId, UpdateStatusArgs args)
{
...
}
[DELETE("Statuses/{siteId}")]
public HttpResponseMessage Delete(string siteId)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
}
AttributeRouting.Core,AttributeRouting.Core.Http,AttributeRouting.Core.Web,AttributeRouting.WebApi的3.5.6版本
Version 3.5.6 of AttributeRouting.Core, AttributeRouting.Core.Http, AttributeRouting.Core.Web, AttributeRouting.WebApi
MVC4
WebDAV是没有安装。
WebDAV is not installed.
推荐答案
你们看到的是预期的行为。在默认的Web API动作选择假定行动是动词的 POST
如果操作名称不具有preFIX包含获取,邮报的动词,放,删除等。
What you are seeing is an expected behavior. Action Selector in Web API by default assumes the action to be of verb POST
if the action name does not have a prefix with verbs like "Get", "Post", "Put", "Delete" etc.
现在,即使你已经指定它不工作 [PUT(的情况/ {}网站ID)]
属性明确,因为,行动选择查找从属性的 System.Web.Http
命名空间像HttpGetAttribute,HttpPostAttribute,HttpPutAttribute等。
Now it isn't working even if you have specified [PUT("Statuses/{siteId}")]
attribute explicitly because, Action selector looks for attributes from System.Web.Http
namespace like HttpGetAttribute, HttpPostAttribute, HttpPutAttribute etc.
由于AttributeRouting的PUTAttribute是上述类型的不是,行动选择不考虑它,仍然认为它是默认的,这是 POST
。所以你具有解决办法 HttpPut
属性是正确的。
Since AttributeRouting's PUTAttribute isn't of the above types, Action selector doesn't consider it and still thinks it to be the default one, which is POST
. So your workaround of having HttpPut
attribute is correct.
这篇关于405使用AttributeRouting.PUTAttribute时,除非我还包括HttpPutAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!