Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Routing - How do I get the URL when inside the controller

Can someone tell me how I can get the URL that was used to call my route when I am in the controller? It seems simple but I can't find any reference on how to do it. If u need an example I can explain more .. Previously I asked a route question and someone told me how I could check which route was met. This time my needs are a bit different.

Thanks,

Mandy

like image 600
Mandy Weston Avatar asked Dec 12 '22 14:12

Mandy Weston


2 Answers

Use the Url property of the Request object.

public ActionResult MyAction()
{
    var url = Request.Url;

    /// .....

    return View();    
}

That will return a Uri object with everything you need.

You might also be interested in the controller's RouteData property, which provides more detailed information about the parsed route.

like image 118
3Dave Avatar answered Mar 16 '23 13:03

3Dave


Since you have a reference to the Request property of Controller, you can just do:

var url = Request.Url.ToString();
like image 33
alexn Avatar answered Mar 16 '23 12:03

alexn