Asp.Net Mvc Route problem
I have been testing a few options with Route Debugger but no luck. Let me describe what I am trying:
I have all routes "translated" as follows (I needed to translate from English to Portuguese and sometime simplify):
routes.MapRoute("Article.Create", "cms/artigo/criar",
new { controller = "Article", action = "Create" });
Everything is working. Since I have all routes defined I think I don't need Default one. So I have something like:
// Other translation routes
routes.MapRoute("Article.Create", "cms/artigo/criar",
new { controller = "Article", action = "Create" });
routes.MapRoute("Article.Edit", "cms/artigo/editar/{id}",
new { controller = "Article", action = "Edit", id = "" });
routes.MapRoute("Home.Index", "inicio",
new { controller = "Home", action = "Index" });
routes.MapRoute("Home.Contact", "contacto",
new { controller = "Home", action = "Contact" });
// Error Unknown
routes.MapRoute("Error.Unknown", "erro",
new { controller = "Error", action = "Unknown" });
// Start
routes.MapRoute("Start", "_Default.aspx",
new { controller = "Home", action = "Index" });
// Error NotFound
routes.MapRoute("Catch.All", "{*path}",
new { controller = "Error", action = "NotFound", path = "" });
If the root goes through the translation mapping with no match then if it is an Unknown Error displays only "http://domain.com/erro"
If not then try to match开发者_如何学运维 Home/Index. Finally goes to catch all route and display only the path as follows "http://domain.com/the/invalid/pathOn my Web.Config I have:
<customErrors mode="On" defaultRedirect="~/Error/Unknown">
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
What happens is when I start my site it always goes to NotFound error and not to start page.
What am I doing wrong?
You have defined that the only URL, that points to your home page, is "~/yourapp/_Default.aspx", but your start url is ""~/yourapp/", which will correctly be intercepted by your Catch.All route. You must change your Start route to this:
MapRoute("Start", "", new { controller = "Home", action = "Index" });
Btw.: If you want to unit test your routes, you may be interested in this: Unit testing ASP.NET MVC routes. I presents a test fixture to test routes in a tabular style - ideal if you have many possible routes (sample code available).
精彩评论