Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC: How do I get virtual url for the current controller/view?

Tags:

Is it possible to get the route/virtual url associated with a controller action or on a view? I saw that Preview 4 added LinkBuilder.BuildUrlFromExpression helper, but it's not very useful if you want to use it on the master, since the controller type can be different. Any thoughts are appreciated.

like image 792
Jim Geurts Avatar asked Sep 30 '08 06:09

Jim Geurts


People also ask

How can get current URL in MVC controller?

If you want to retrieve the URL of the current Page in a ASP.NET MVC 4 Controller , you can get it by using the AbsoluteUri property defined in the Request. Url class.

How do you access the variable of the controller in the view?

OK, the simple answer is, you need to know the . Net type being returned from your query when you set the scrap variable in your controller. Whatever that type is, that's what type your @model needs to be. Either that, or use the ViewBag --but again, without IntelliSense support since it's a dynamic .

How do you give a controller a URL?

If you just want to get the path to a certain action, use UrlHelper : UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u.

How do I redirect a view from a controller?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.


1 Answers

I always try to implement the simplest solution that meets the project requirements. As Enstein said, "Make things as simple as possible, but not simpler." Try this.

<%: Request.Path %>
like image 179
Tarzan Avatar answered Oct 20 '22 13:10

Tarzan