40

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

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

5 Answers5

101

You could use the following if you have access to the HttpContext:

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
L01NL
  • 1,753
  • 1
  • 13
  • 17
  • 4
    This 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
  • 1
    Was 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
  • 8
    worth 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
6

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

2

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

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
1

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() %>
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
-5

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
}
Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
  • 32
    umm, if you already have the UrlHelper, why call this method? – hunter Feb 05 '11 at 15:15
  • @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
  • 1
    Cant 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