Alex Rivera | Logout

Generating url for a resource in asp.net web api outside of ApiController

Asked 2012-05-07T23:51:55.263
10

Looking for a way to construct or generate a url for a specific resource in asp.net web api. It can be done in the controller since it inherits from ApiController hence you get the UrlHelper.

I am looking to construct resource url out of the context of the ApiController.

Edit
Report

1 Answer

-1

I'm not sure about the ApiController, as I haven't used it before. This may then be redundant for you, but then again, it may not be. Check out your Global.asax.cs file, specifically the RegisterRoutes function. Initially, you should see the following mapping:

  routes.MapRoute ("Default", "{controller}/{action}/{id}", new { controller = "MyController", action = "Index", id = "" });

So by default your application is set up to handle routes in the following format:

 {ControllerName}/{ActionName}/{ResourceId}

A controller class set up like the following should enable you to receive requests in that format.

 class {ControllerName}Controller : ApiController
 {
      public ActionResult {ActionName} (string id)
      {
           // fetch your resource by its unique identifier
      }
 }
answered 2012-05-08T00:02:23.200

Your Answer