Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 when using AttributeRouting.PUTAttribute unless I also include HttpPutAttribute

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.

like image 579
Thad Avatar asked Jun 19 '13 16:06

Thad


1 Answers

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.

like image 143
Kiran Avatar answered Oct 17 '22 19:10

Kiran