开发者

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

开发者 https://www.devze.com 2023-02-19 06:26 出处:网络
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 exampl

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


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.


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

var url = Request.Url.ToString();


I would use the RouteData.Values property instead of the Request property. The Request property will probably be null in the unit testing scenario.


You can use the Routing debugger to see what url matches your controller / actions

more information

0

精彩评论

暂无评论...
验证码 换一张
取 消