Routes Information
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
routes.MapRoute(
"Default1", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Url Information
http://localhost:24060/home/22323 //Failed
http://localhost:24060/home/index/22323 //Passed\
Query, How can i pass both url ?
You have to map the default route last. Also you should create a constraint in the other route to not to block the default route.
routes.MapRoute(
"Default1",
"{controller}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { id = #"\d+" });
//second segment has to be an integer, otherwise skip this route.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Related
I have two controllers, HomeController and ResourcesController.
I want to hide the Home/ from the url when action on HomeController is requested, but for ResourcesController (or any other controlelr) I want to keep the controller name in url.
E.g. /Home/Products will be /Produtcs, but /Resources/Banana should stay /Resources/Banana
These are my routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SpecificRoute",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Home", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Home", id = UrlParameter.Optional }
);
It works as expected for home controller but for resources controller I get "404... The resource cannot be found"
One possible way is you can map all the home page actions within the global.asax file. See the example code below.
e.g.
routes.MapRoute(
"ProdutcsRoute", // Route name
"Produtcs/{id}", // URL with parameters
new { controller = "Home", action = "Produtcs", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"AboutRoute", // Route name
"About/{id}", // URL with parameters
new { controller = "Home", action = "About", 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
);
I have two routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "CustomRoute",
url: "{country}/{lang}/{controller}/{action}",
defaults: new { controller = "Test", action = "Index" }
);
but i only can acces to my CustomRoute with something like /ES/es/Test/Action if I try with something like /ES/es/ I have 404 error, it doesn´t find the page, and I really need the /ES/es/ way. Some ideas why is not taking default values in CustomRoute ? ... thanks in advance.
You can see sample code:
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
//);
routes.MapRoute(
"CustomRoute", // Route name
"{country}/{lang}/{controller}/{action}", // URL with parameters
new { country = "MyContry", lang = "Mylanguage", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Please refer this links, hope this could help you..
https://codereview.stackexchange.com/questions/6543/custom-route-for-writing-friendly-urls-in-asp-net-mvc-3
http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx
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 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?
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
);