Reputation: 1941
I've got a MVC Application with only the default route specified as follows:
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
First of all, I'd like to understand the meaning of the word "Default" in the first parameter of this route, in order to build a route that can work as I intend it to.
With the following, I wanted to be able to invoke the index of the ApplicationModule controller without having to specifically mention the index method in the Url, much like I do for the other controllers:
href="<%= ResolveUrl("~/") %>ApplicationModule/Index/<%= Html.Encode(app.id) %>"
If I remove the /Index from this Url, the application tells me that the said resource was not found. What am I doing wrong?
Upvotes: 1
Views: 109
Reputation: 18474
Answer to 1: "Default" is just a name for the route for your internal reference, helps you describe what the route is for.
Answer to 2: Add this mapping in BEFORE the default one.
routes.MapRoute( _
"ApplicationModuleIndex", _
"ApplicationModule/{id}", _
New With {.controller = "ApplicationModule", .action = "Index", .id = UrlParameter.Optional} _
)
This may have the side effect that the url "ApplicationModule/Action" (ie no id) may just call the index action instead and you may need an addition route (before the previous one) of
routes.MapRoute( _
"ApplicationModule", _
"ApplicationModule/{action}", _
New With {.controller = "ApplicationModule", .action = "Index"} _
New With {.action = "Index|Action1|Action2"} _
)
Replace the "Index|Action1|Action2"
with a |
seperated list of actions that are valid
Alternatively you could just dump adding these new routes and override the HandleUnknownAction
eg.
in your ApplicationModule controller
protected override void HandleUnknownAction(string actionName) {
var result = Index(actionName);
result.ExecuteResult(ControllerContext);
}
Upvotes: 0
Reputation: 9296
Understanding the routing mechanism takes practice and experience. The idea behind routing mechanism is to take the incoming URL and obtain the handler (IHttpHandler) object that will handle the request. As for your question, the first parameter of MapRoute extension method is the name of a route. When you set up an MVC project you get:
routes.MapRoute( _
' "Default" is your route name. It can be null, "" or some other name that you give it
"Default", _
' This is your URL with parameters
"{controller}/{action}/{id}", _
' Parameter defaults
new { .controller = "Home", .action = "Index", .id = UrlParameter.Optional })
The route name paremeter allows your to invoke the controller and action just by using the name. For example, instead of:
return RedirectToAction("ActionName", "ControllerName", new { paramId = 1234 });
you can use route name like
return RedirectToRoute("MyRouteName", new { paramId = 1234 });
As for the configuration of the route it should be something like this:
routes.MapRoute( _
"YourRouteName", _
"ApplicationModule/{id}", _
New With {.controller = "ApplicationModule", .action = "Index", .id = UrlParemeter.Optional })
And place this new route configuration above the default one that MVC gives you when you create a project, otherwise your new route configuration will never be caught and the default one will always attempt to resolve incoming URL's.
And one more suggestion for the end, instead of using:
href="<%= ResolveUrl("~/") %>ApplicationModule/Index/<%= Html.Encode(app.id) %>"
use Url.Content like this:
href="<%= Url.Content("Index", "ApplicationModule", With New { .id = app.id })"
This will automatically resolve your virtual path and looks a bit neater. If you get a chance to get hold of Steven Sanderson's Pro ASP.NET MVC 2 Framework, you will get a much clearer picture about URLs and routing mechanism.
Best regards,
Huske
Upvotes: 1
Reputation: 35793
Default is just the name of the route.
You can set up a route that will do this, but it will likely break the rest of your site.
The problem is that you want it to automatically treat the 2nd parameter in the URL as the ID, but how does it know that it is an ID rather than an action method name?
Upvotes: 1