0

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?

MrBliz
  • 5,830
  • 15
  • 57
  • 81

3 Answers3

0

Only guessing, but I think WebAPI cannot distinguish between route 1 and 2 because "comment" could also be a user-name. You could change the first route to something like:

[Route("{jobId}/users/{user}"), System.Web.Http.HttpPost]
public HttpResponseMessage AssignUser(long jobId, string user)
Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64
0

The problem here, is that both actions have the same action (POST), the same number of parameters and same type for these parameters, so which comes first that matches the route will win.

Your route need to have something unique distinguish between the two routes, so you need to change your routes to either have different actions (POST vs PUT for example) or you change one route to be something like this

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

hope this helps.

Omar.Alani
  • 4,050
  • 2
  • 20
  • 31
  • if i change the second one to have a route of [Route("{id}/comment/add")] then hitting the route 'http://mydomain/API/Job/11/Comment/add'. WEB Api responds with "No action was found on the controller 'Job' that matches the request." – MrBliz Dec 18 '14 at 13:00
  • I know that would work as a quick fix, but is it really wise to be putting the information you want to change on the server onto the URL? – MrBliz Dec 18 '14 at 13:42
  • 1
    Normally you pass your data via querystring ONLY with GET operation, for the others (PUT, POST, DELETE, PATCH) you use message body to pass your data to the server ? does this answer your question ? – Omar.Alani Dec 19 '14 at 01:08
0

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; }
}
MrBliz
  • 5,830
  • 15
  • 57
  • 81