Search code examples
asp.net-web-apiasp.net-web-api2asp.net-web-api-routing

ASP.NET Web Api 2 routing issue


I really can't understand why it does not work. I have the following code:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

[RoutePrefix("api/Profile")]
[System.Web.Http.AuthorizeAttribute]
[IdentityBasicAuthenticationAttribute]
public class ProfileApiController : ApiController
{
    [HttpPost]
    [ValidateApiContentLength]
    [ValidateApiMimeMultipart]
    [Route("Upload")]
    public async Task<HttpResponseMessage> UploadDocumentAsync(string description)
    {
       //....
    }
}

}

but when I call: http://localhost:11015/api/profile/Upload

I get 404 error:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:11015/api/profile/Upload'.",
    "MessageDetail": "No action was found on the controller 'ProfileApi' that matches the request."
}

but insight says about error:

enter image description here

what is incorrect?


Solution

  • I have found a solution. Problem was not in the routing. Problem was in parameter of action. It should not be there for POST method. Other things leave as is

        [HttpPost]
        [ValidateApiContentLength]
        [ValidateApiMimeMultipart]
        [Route("upload")]
        public async Task<HttpResponseMessage> UploadDocumentAsync()
        {