ASP.NET MVC - how to get a full path to an action

dev.e.loper picture dev.e.loper · Jul 22, 2011 · Viewed 28.7k times · Source

Inside of a View can I get a full route information to an action?

If I have an action called DoThis in a controller MyController. Can I get to a path of "/MyController/DoThis/"?

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 22, 2011

You mean like using the Action method on the Url helper:

<%= Url.Action("DoThis", "MyController") %>

or in Razor:

@Url.Action("DoThis", "MyController")

which will give you a relative url (/MyController/DoThis).

And if you wanted to get an absolute url (http://localhost:8385/MyController/DoThis):

<%= Url.Action("DoThis", "MyController", null, Request.Url.Scheme, null) %>