Search code examples
asp.netasp.net-web-apiattributerouting

Attribute Routing failing - Web Api


I have two actions on a controller called JobController that has this Route Prefix

[RoutePrefix("API/Job")]

1st Action (In order of precedence in controller)

[Route("{jobId}/{user}"), System.Web.Http.HttpPost]
public HttpResponseMessage AssignUser(long jobId, string user)

2nd Action

[HttpPost]
[Route("{id}/comment/")]
public HttpResponseMessage SaveComment(string commentText, long id)

Doing a post with Postman to this Route - MyDomain/API/Job/11/Comment - with a commentText value of "foo" matches against the first route, not the one i want it to.

Any ideas why this is happening?


Solution

  • In the end I just created separate ViewModels

    [HttpPost]
    [Route("comment")]
    public HttpResponseMessage SaveComment([FromBody] JobCommentViewModel viewModel)
    
    public class JobCommentViewModel
    {
        public long JobId { get; set; }
        public string Comment { get; set; }
    }