MVC3 Routing - How to get the URL inside the controller

Can someone tell me how can I get the url that was used to call my route when I am in the controller? It seems simple, but I cannot find any links on how to do this. If you need an example, I can explain more. I used to ask a question about a route, and someone told me how I can check which route was completed. This time my needs are a little different.

Thanks,

Mandy

+4
source share
4 answers

Use the Url property of the Request object .

public ActionResult MyAction() { var url = Request.Url; /// ..... return View(); } 

This will return a Uri object with everything you need .

You may also be interested in monitoring the RouteData property , which provides more detailed information about the route being analyzed.

+6
source

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

 var url = Request.Url.ToString(); 
+5
source

I would use the RouteData.Values โ€‹โ€‹property instead of the Request property. The Request property is likely to be null in the unit test script.

+1
source

You can use the routing debugger to find out which URL matches your controller / actions.

Additional Information

0
source

Source: https://habr.com/ru/post/1345109/


All Articles