405 when using AttributeRouting.PUTAttribute unless I also include HttpPutAttribute

a 夏天 提交于 2019-12-12 11:11:03

问题


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);
        }
}

Version 3.5.6 of AttributeRouting.Core, AttributeRouting.Core.Http, AttributeRouting.Core.Web, AttributeRouting.WebApi

MVC4

WebDAV is not installed.


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/17196731/405-when-using-attributerouting-putattribute-unless-i-also-include-httpputattrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!