2

Microsoft has a good tutorial for getting your first ASP.NET Web API project going:

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

...but what's unclear to me -- how is the mapping between the API URIs and my C# controller methods defined?

for ex:

/api/products/{id}

resolves to

public IHttpActionResult GetProduct(int id)
{
    [...]
}

...but im not sure how. automagic?

I ask because I want to create a new mapping for this URI:

/api/setReportNotificationsAsRead?uid={username}&items={itemIDs}

to this new method in my controller:

public IHttpActionResult SetReportNotificationsAsRead(string username, string itemIDs)
{
    [...]
}
mdelvecchio
  • 567
  • 1
  • 5
  • 25

2 Answers2

4
...but im not sure how. automagic?

The Automagic lies with HTTP Verbs in Web API. The route /api/products/{id} can map to anything that starts with GET (or just method name Get) if you make a GET request for the WebAPI.

If you make a POST request then it will map to any POST method (or else is you use route prefix).

This link can help you understand the magic. That can also help you understanding how you can configure SetReportNotificationsAsRead function to be called.

Guanxi
  • 3,103
  • 21
  • 38
  • please do you think you could help me with this question http://stackoverflow.com/questions/24235617/asp-net-web-api-what-problems-could-arise-if-i-use-post-instead-of-put-and-del/24235652#24235652 – eddy Jun 16 '14 at 02:49
2

You're very close to having a working solution. You have your method parameter names mixed up:

public IHttpActionResult SetReportNotificationsAsRead(string uid, string items)
{
    //Do your stuff!
}

The name of the HTTP GET parameter should match the name of the method parameter. This is part of the ASP.NET MVC naming convention.

One final note, if you are actually expecting a group of items (i.e. multiple item ids) you can actually save yourself some work and use a string[] parameter so long as your HTTP parameters are sent correctly. In this case you would want the URI to look like: /api/setReportNotificationsAsRead?items=123&items=456&items=789. Notice how the parameter name is repeated multiple times, but with different values.

For more information about the routing system I would suggest heading over to the ASP.NET MVC routing overview.

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • ah yeah, so the URI querystring parameters have to match the method param names exactly. got it. is there any other hard-mapping, or do you just do them by ensuring the URI and API names match? ill read your link, probably says... interesting on the string[] option. will evaluate. the db ultimately takes in a simple XML of 12, so i can either pass that into the API, or build it inside the API method... – mdelvecchio May 30 '14 at 21:29
  • 1
    @mdelvecchio - By default you can see how your application routes are setup by looking at the `RouteConfig.cs` file. Since it looks like you're using Web API 2, you can even take that further and define [routes per method with attributes](http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2). – Justin Helgerson May 30 '14 at 21:35
  • thanks - using Web API 2 attributes is definitely the way to go, for me. it was pretty easy to use [decorators] to define the URI above a controller method...presto, mapped. easier than working in the RouteConfig.cs. – mdelvecchio Jun 05 '14 at 15:07