rekna
rekna

Reputation: 5333

ASP.MVC 3 routing : how to get url with default action included?

Suppose I have the following routing

routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

Now, when you generate an url using Url.Action("Index","MyController") you will get as expected : /MyController

But in one exceptional case, I would like to get the full url /MyController/Index (without changing the routing)... does anyone know if this is possible?

Upvotes: 0

Views: 476

Answers (2)

jgauffin
jgauffin

Reputation: 101140

It is possible. But you need to modify the routing.

  1. Create an own routing class that derives Route
  2. Override the GetVirtualPath() method to include /index for the pages that needs it.
  3. Configure the default route using your routing class instead.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

I am afraid this is not possible. And it shouldn't matter as both urls will resolve to the same controller action.

Upvotes: 1

Related Questions