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.
Here is what I did:
// 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);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With