How do I generate a URL outside of a controller in ASP.NET MVC?

28,393

Solution 1

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
}

Solution 2

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

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

Solution 3

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

Solution 4

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

Solution 5

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() %>
Share:
28,393
Shawn Mclean
Author by

Shawn Mclean

:)

Updated on December 09, 2020

Comments

  • Shawn Mclean
    Shawn Mclean over 3 years

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

  • hunter
    hunter about 13 years
    umm, if you already have the UrlHelper, why call this method?
  • Mahesh Velaga
    Mahesh Velaga about 13 years
    @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.
  • Shawn Mclean
    Shawn Mclean about 13 years
    Cant I access UrlHelper from a static class or something built in asp.net mvc? Without passing the UrlHelper.
  • bytedev
    bytedev over 7 years
    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.
  • Daniel Park
    Daniel Park almost 7 years
    Was able to provide via injection (Autofac) with the following line; builder.Register(context => new UrlHelper(context.Resolve<RequestContext>())).InstancePerReq‌​uest();
  • Hakan Fıstık
    Hakan Fıstık over 6 years
    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
  • Florian Winter
    Florian Winter about 6 years
    Not useful to implement, e.g., a read-only Url property in a model class.