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.

3 Answers 3

2

Here is what I did:

  • Requires HttpContext/Request, so might not work in Application_Start.
  • Only tested in WebApi 1
  • Only works for routes registered in GlobalConfiguration (but if you have some other one, just pass it in instead)
// given HttpContext context, e.g. HttpContext.Current var request = new HttpRequestMessage(HttpMethod.Get, context.Request.Url) { Properties = { { HttpPropertyKeys.HttpConfigurationKey, GlobalConfiguration.Configuration }, { HttpPropertyKeys.HttpRouteDataKey, new HttpRouteData(new HttpRoute()) }, { "MS_HttpContext", new HttpContextWrapper(context) } } }; var urlHelper = new UrlHelper(request); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Andrey, almost give up with this one. Turns out HttpPropertyKeys.HttpRouteDataKey is mandatory for Mvc.UrlHelper. Great "Спасибо" )!
When I follow this, my UrlHelper is created but it generates Url's with 127.0.0.1 as the host name. What type of configuration did you do to get this to generate the proper host name? And is there a way to get it to generate https rather than http?
@GregVeres What's in your context.Request.Url?
0

What about the UrlHelper classes:

System.Web.Http.Routing.UrlHelper; System.Web.Mvc.UrlHelper 

The MVC one has some useful static methods accepting routing information or it can be used as an instance created by passing in a RequestContext (which is available in most MVC filters and various other places). The instance methods should be exactly what you need to generate urls.

The HTTP one accepts a ControllerContext (which is also available in most HTTP filters and various other places).

2 Comments

System.Web.Http.Routing.UrlHelper now only takes a single HttpRequestMessage which it uses on calls to Link to find the server name / domain to use at the start of the generated Url. The HttpRequestMessage also gets passed down to the GetVirtualPath method of HttpRouteCollection. So it seems that System.Web.Http.Routing.UrlHelper doesn't help us here.
To the downvoters, an alternative would be nice - considering this was totally valid at the time. I'll leave this answer simply because the comments it now has have value.
-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 } } 

1 Comment

what i am trying to find is a way to derive a url given a resource outside of ApiController. Basically based on the resource, its Id, and some HttpContext, programmatically resolve the absolute url for that resource. This is for creating REST service using asp.net web api.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.