ASP.NET MVC extract controller, action and value from URL

835 Views Asked by At

I m writing an httpModule to intercept the request that is coming to ASP.NET MVC application. Is there an easy way to figure out what part of the URL constitutes area, controller, action and the actual value.

e.g. www.mysite.com/category/products/GetDetails/101

category: area products: controller GetDetails: action 101: productId

Is there an easy way to get back from RouteEngine or something...

2

There are 2 best solutions below

1
On

ControllerContext.RouteData

Should help you out, the answer here too.

0
On

A route like this might work:

routes.MapRoute(
            "category", // Route name
            "~/category/products/getdetails/{ProductId}", // URL with parameters
            new { controller = "products", action = "getdetails" },
            new { ProductId = "\\w+" });

This will map the ProductId as a variable to the getdetails action of your products controller.