Ian Vink
Ian Vink

Reputation: 68750

Getting a Url from inside a MVC Controller

In my MVC 5 Controller I generate an email and inside is a link. I need to get the full url to an specific action.

Is there a way to generate the full URL in the controller for a given action+route ?

Upvotes: 0

Views: 1031

Answers (2)

Mehdi Souregi
Mehdi Souregi

Reputation: 3265

For Full URL:

var pageUri = new Uri(HttpContext.Request.Url, Url.Action("ActionName", "ControllerName"));

Upvotes: 0

Ryan Erdmann
Ryan Erdmann

Reputation: 1826

Take a look at the documentation for the UrlHelper class: http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx

You can simply do:

var url = Url.Action("ActionName", "ControllerName");

You can also pass along route parameters in an object in the third parameter:

var url = Url.Action("ActionName", "ControllerName", new { id = 123 });

Upvotes: 3

Related Questions