1

How do I generate a URL pointing to a controller action from a helper method outside of the controller in ASP.net WebApi 2?

return new UrlHelper().Link("DefaultApi", new
{
    Controller = "MyController",
    Action = "MyAction"
});

UPDATE 1
Generating URL in WebApi is different from ASP.net MVC

UPDATE 2
Here is route config:

config.Routes.MapHttpRoute(
    "DefaultApi",
    "API/{Controller}/{Action}"
);
  • Possible duplicate of [How do I generate a URL outside of a controller in ASP.NET MVC?](https://stackoverflow.com/questions/4907540/how-do-i-generate-a-url-outside-of-a-controller-in-asp-net-mvc) – Masoud Keshavarz May 08 '19 at 08:51
  • No, Generating URL in WebApi is different from ASP.net MVC –  May 08 '19 at 13:09
  • Yes its quite different but strategy is the same. Just pass the `HttpRequestMessage` to the helper class and generate link. Please see my answer. – Masoud Keshavarz May 08 '19 at 16:13

1 Answers1

0

Here is how I overcame this:

RouteConfig

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { id = RouteParameter.Optional }
);

Helper Class

public static string GenerateLink(HttpRequestMessage httpRequest)
{
    var urlHelper = new UrlHelper(httpRequest).Route("DefaultApi", new
    {
        Controller = "MyController",
        Action = "MyAction"
    });

    return httpRequest.RequestUri.Scheme + "://" + httpRequest.RequestUri.Authority + urlHelper;
}

And finally the api controller which calls the helper class:

public class DefaultController : ApiController
{
    [HttpGet]
    public string Default()
    {
        var link = HelperClass.GenerateLink(Request);
        return link;
    }
}
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
  • In this example UrlHelper return null –  Jul 05 '19 at 04:47
  • @Mehran It is critical to pass `Request` into `GenerateLink()` method like `HelperClass.GenerateLink(Request)`. Otherwise `HttpRequestMessage httpRequest` will be null inside `GenerateLink()` method – Masoud Keshavarz Jul 05 '19 at 06:46
  • Yes, I pass Request into GenerateLink method like HelperClass.GenerateLink(Request) but HttpRequestMessage httpRequest is null –  Jul 09 '19 at 07:26
  • @Mehran Well, this is another problem and you must post a new question and show your code structure and ask why `Request` is `null` inside controller. Or at least please update your current question and address the new problem. – Masoud Keshavarz Jul 09 '19 at 07:53
  • I apologize for the wrong answer, Request is not null in controller! in fact new UrlHelper(httpRequest).Route(...) return null –  Jul 10 '19 at 07:19
  • `.Route(...)` must be initialized according to your `RouteConfig` file. Please update your question and show us your route config. Maybe your route name is different than `"DefaultApi"` or your template is different than `"api/{controller}/{action}"` – Masoud Keshavarz Jul 10 '19 at 08:13
  • My mistake again –  Jul 11 '19 at 10:30