jhappoldt
jhappoldt

Reputation: 2526

How can I generate a link to a resource not in the WebAPI routing table?

I'm attempting to make a Virtual directory relative link to some unknown (to WebAPI) resource form an ApiController.

Example:

http://some/path/webapi/does/not/know

The WebAPI Url helper seems to be tightly coupled with Routing and does not have a Content() method like the MVC variant. I'm trying to avoid using any non-mockable HTTP context information to make this call (such as HttpContext.Current).

Thanks for the help!

Upvotes: 2

Views: 414

Answers (2)

sellmeadog
sellmeadog

Reputation: 7517

Here are two options you can try:

  1. HostingEnvironment.MapPath()
  2. Create a System.Web.Mvc.UrlHelper in your API controller

Upvotes: 0

Pablo Cibraro
Pablo Cibraro

Reputation: 3959

You can always do,

var urlBuilder = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = "webapi/does/not/know" };

var uri = urlBuilder.Uri

In that way, you don't need to rely on the UrlHelper. The base url is inferred from the current request.

Upvotes: 1

Related Questions