How do I generate a URL pointing to a controller action from a helper method outside of the controller?

- 57,289
- 29
- 176
- 237

- 56,733
- 95
- 279
- 406
-
possible duplicate of [ASP.NET MVC - Accessing Url.GenerateUrl from Outside the Controller](http://stackoverflow.com/questions/3722352/asp-net-mvc-accessing-url-generateurl-from-outside-the-controller) – Tomas Aschan Feb 05 '11 at 15:12
-
I assume you are not talking about the View's UrlHelper either? – dotjoe Feb 05 '11 at 15:38
-
8None of the answers are truly *an answer* at all. – Saeed Neamati Jun 11 '12 at 11:02
5 Answers
You could use the following if you have access to the HttpContext
:
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

- 16,800
- 14
- 110
- 131

- 1,753
- 1
- 13
- 17
-
4This should really be the correct answer. Just pass the object into the method (current correct answer) is a bit too obvious and is not UrlHelper specific. – bytedev Nov 01 '16 at 17:15
-
1Was able to provide via injection (Autofac) with the following line; `builder.Register(context => new UrlHelper(context.Resolve
())).InstancePerRequest();` – Daniel Park Jun 04 '17 at 22:09 -
8worth to say using the `System.Web.Mvc` namespace and not the `System.Web.Http.Routing`, because this class is exists in those two namespace – Hakan Fıstık Jul 31 '17 at 14:10
You can use LinkGenerator . It's new feature in Microsoft.AspNetCore.Routing namespace and has been added in Aug 2020 .
At first you have to inject that in your class :
public class Sampleervice
{
private readonly LinkGenerator _linkGenerator;
public Sampleervice (LinkGenerator linkGenerator)
{
_linkGenerator = linkGenerator;
}
public string GenerateLink()
{
return _linkGenerator.GetPathByAction("Privacy", "Home");
}
}
For more information check this

- 439
- 1
- 7
- 14
Using L01NL's answer, it might be important to note that Action method will also get current parameter if one is provided. E.g:
editing project with id = 100
Url is http://hostname/Project/Edit/100
urlHelper.Action("Edit", "Project")
returns http://hostname/Project/Edit/100
while urlHelper.Action("Edit", "Project", new { id = (int?) null });
returns http://hostname/Project/Edit

- 23,328
- 24
- 73
- 116

- 22,016
- 16
- 145
- 164
Since you probably want to use the method in a View, you should use the Url
property of the view. It is of type UrlHelper
, which allows you to do
<%: Url.Action("TheAction", "TheController") %>
If you want to avoid that kind of string references in your views, you could write extension methods on UrlHelper
that creates it for you:
public static class UrlHelperExtensions
{
public static string UrlToTheControllerAction(this UrlHelper helper)
{
return helper.Action("TheAction", "TheController");
}
}
which would be used like so:
<%: Url.UrlToTheControllerTheAction() %>

- 58,548
- 56
- 243
- 402
Pass UrlHelper to your helper function and then you could do the following:
public SomeReturnType MyHelper(UrlHelper url, // your other parameters)
{
// Your other code
var myUrl = url.Action("action", "controller");
// code that consumes your url
}

- 21,633
- 5
- 37
- 59
-
32
-
@hunter: OP has a helper, he needs the url in that helper, I am suggesting that passin UrlHelper to his helper to generate the url he wants. I edited my answer to make it more clear on what I am trying to suggest. – Mahesh Velaga Feb 05 '11 at 15:19
-
1Cant I access UrlHelper from a static class or something built in asp.net mvc? Without passing the UrlHelper. – Shawn Mclean Feb 05 '11 at 23:05
-
Not useful to implement, e.g., a read-only Url property in a model class. – Florian Winter Mar 09 '18 at 10:19