I have this RegisterRoutes method in Global.asax, can these be abbreviated with giving the same reslut? and what about their order?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Villages", // Route name
"villages", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"CreateVillage", // Route name
"villages/create", // URL with parameters
new { controller = "Villages", action = "Create", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"", // Route name
"", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
}
You can combine first with fourth route and second with third. The defualt route might also be combined with second and third. Note that you'll probably need to use some route constraints in order to make this table work properly. You can experiment with route debugger, but I would suggest you write unit tests for your routes.
Edit:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Villages", action = "List", name = UrlParameter.Optional }, // Parameter defaults
new { action = "create|list"}
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
Related
For some reason, my route isn't being seen by .NET.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"RSSFeed", // Route name
"Blog/RSSFeed", // URL with parameters
new { controller = "Blog", action = "RSSFeed", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"About", // Route name
"About/", // URL with parameters
new { controller = "About", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Contact", // Route name
"Contact/", // URL with parameters
new { controller = "Contact", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Admin", // Route name
"admin/", // URL with parameters
new { controller = "Admin", action = "CreatePost", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Resume", // Route name
"resume/", // URL with parameters
new { controller = "Resume", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"ViewArticle", // Route name
"{controller}/{action}/{id}/{friendly}", // URL with parameters
new { friendly = "" } // Parameter defaults
);
routes.MapRoute(
"DownloadResume", // Route name
"DownloadResume/", // URL with parameters
new { controller = "Resume", action = "DownloadResume", id = UrlParameter.Optional } // Parameter defaults
);
//Any of these
routes.MapRoute(
"cpe", // Route name
"Play/CreatePlayEvent/{groupid}/{username}/{activeplay}/{game}", // URL with parameters
new { controller = "Play", action = "CreatePlayEvent", groupID = "0", username = "", activeplay = "", game = "" } // Parameter defaults
);
routes.MapRoute(
"CheckPlayEvent", // Route name
"{controller}/{action}/{groupID}", // URL with parameters
new { controller = "Play", action = "CheckPlayEvent", groupID = "0" } // Parameter defaults
);
routes.MapRoute(
"clpe", // Route name
"Play/ClearPlayEvent/{groupid}/{username}", // URL with parameters
new { controller = "Play", action = "ClearPlayEvent", groupID = "0", username = "" } // Parameter defaults
);
//End broken routes
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
myurl.com/Play/CheckPlayEvent/0
Group ID keeps being seen as NULL on the controller side. Anyone? Also, there doesn't seem to be a built-in debugger for routing, or at least I haven't seen one. What does everyone use to debug routes?
Is there a way to say that the Controller name, method name, and parameters make the URL?
I think the following route is getting hit, rather that your intended route:
routes.MapRoute(
"ViewArticle", // Route name
"{controller}/{action}/{id}/{friendly}", // URL with parameters
new { friendly = "" } // Parameter defaults
);
Friendly is optional, so {controller}/{action}/{id} style routes will also hit this - which looks really quite like the route you are having trouble with.
Move your play route above this one and try again.
I am localizing my MVC3 site based on the route data. for example, http://domain/fr should display the site in French and http://domain should default to english...below is how i registered my routes in Global.ascx.
My issue is that http://domain/fr/Home/Index will work, but http://domain/Home/Index will display resource not found error, and with an investigation it tells me the route table is mapping "Home" to {lang}
What am I missing?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"LogOn", // Route name
"Account/{action}", // URL with parameters
new { controller = "Account", action = "LogOn" } // Parameter defaults
);
routes.MapRoute(
"Localization", // Route name
"{lang}/{controller}/{action}", // URL with parameters
new { UrlParameter.Optional, controller = "Home", action = "Index"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
}
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index"}, // Parameter defaults
new { controller = "[a-zA-Z]{3,}" } //regexp constraint on controller name
);
routes.MapRoute(
"Localization", // Route name
"{lang}/{controller}/{action}", // URL with parameters
new { UrlParameter.Optional, controller = "Home", action = "Index"} // Parameter defaults
);
Should do the trick, provided all your controller names are longer than 2 characters :)
I have a Controller named HomeController, a folder named Home, and a View called Index. I also have another Controller named TestEditController, a folder named TestEdit, and a View called Index. For some reason, when I compile it the URL: http://localhost:4097/ doesn't point to Home/Index but to TestEdit/Index. I went to the Properties > Start Action > Specific Page ... and left the textbox blank. Note: putting a / doesn't work. I've cleaned, build, rebuild the project/solution. But still getting the same issue. Here's my Global.asax files:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SectionsData", // Route name
"{controller}/{action}/{id}/{prodno}/{instid}/{section}", // URL with parameters
new { controller = "TestEdit", action = "Sections", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Sections", // Route name
"{controller}/{action}/{id}/{prodno}/{instid}", // URL with parameters
new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"MainProducts", // Route name
"{controller}/{action}/{id}/{prodno}", // URL with parameters
new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Catalogs", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Products", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Your matching is too generic. try this instead:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SectionsData", // Route name
"TestEdit/Sections/{id}/{prodno}/{instid}/{section}", // URL with parameters
new { controller = "TestEdit", action = "Sections", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Sections", // Route name
"TestEdit/Index/{id}/{prodno}/{instid}", // URL with parameters
new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"MainProducts", // Route name
"Home/Main/{id}/{prodno}", // URL with parameters
new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Catalogs", // Route name
"Home/Products/{id}", // URL with parameters
new { controller = "Home", action = "Products", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
...but really, you don't need a lot of these routes.
Because both SectionsData and Sections Route has all other parameters optional, they will match before the default route.
I have global.ascx with three routes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
routes.MapRoute(
"TestCatalogRoute",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
//new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
);
}
When I enter url:
http://mydomain.com/
It uses "TestCatalogRoute" route, but I want "Default" route T.T
How to:
with url: http://mydomain.com it uses "Default" route
with url: http://mydomain.com/1 it uses "TestRoute" route (It's already done!)
with url: http://mydomain.com/abc it uses "TestCatalogRoute" route
Remove id = UrlParameter.Optional for TestCatalogRoute then
Change the order of your routes. The routehandler will validate each route, first one that matches will get picked. So if you put the second one last, you should be fine?
I can recomend using Routing Debugger to debug your route easy to use.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
I have these routes:
routes.MapRoute(
"ActionOnly",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = "Klub|Historie" });
routes.MapRoute(
"Administrace", // Route name
"Administrace/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// Hrac/Jmeno_hrace
routes.MapRoute(
"Hrac",
"Hrac/{name}",
new { Controller = "Hrac", Action = "Name" }
);
// pro aktivaci uzivatele který se registroval, ale jeste nepotvrdil email
routes.MapRoute(
"Activate",
"Account/Activate/{username}/{key}",
new { controller = "Account", action = "Activate", username = UrlParameter.Optional, key = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Why #Html.ActionLink("Domů", "Index", "Home") is creating website.com/Administrace/Home and not website.com/Home/index and how can I fix it?
Your Administrace route is swallowing all controllers.
You should change it to hard-code the controller name:
routes.MapRoute(
"Administrace", // Route name
"Administrace/Administrace/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If you want that route to work for multiple controllers, you should replace it with an area (or just add a constraint).
Use #Html.RouteLink instead with the route name being default
Link to something that might help:
What's the difference between RouteLink and ActionLink in ASP.NET MVC?