I am learning ASP.NET MVC4 Web APIs. I would like to create a new method that would be which receives two parameters
[HttpPost]
public string MarkAsSeen(int objectID, long userID)
I would like to consume it using jquery:
$.ajax({
type: "POST",
url: "/api/tutorial/MarkAsSeen",
data: JSON.stringify({ objectID: _obj, profileID: _pid }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(_tutorial + ' seen');
},async: true
});
I have my route configured this way
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
But it just doesn't work:
No HTTP resource was found that matches the request URI 'http://local.dev.unation.com/api/tutorial/MarkAsSeen'.","MessageDetail":"No action was found on the controller 'Tutorial' that matches the request.
IF I change the route's template to api/{controller}/{action}/{objectID}/{profileID} and the jquery method to add this info on the url: /api/tutorial/MarkAsSeen/ + _obj + / + _pid it works.
I would not like to perform these changes because:
- I would like to have one general route to all actions (so their parameter names/quantity shouldn't matter)
- This url appending is just ugly, I would like to use the data attribute
Can you tell me what I am doing wrong?
tks!