I'm used to working with WebForms, where i have been using Routing for some time now.
I'm considering migrating from WebForms to MVC, but while i was researching how to build complex routes, i'm left wondering how can i implement the same patterns that i have been implementing for years.
Note that i do not have a 'controller' name, nor an action... i use an url formatted unique name for each category, sub-category and product, so my routes would be something like:
if you don't want the control and action names in the route url, then you just default them. {id} is a just a suggestion for the name of a action parameter, you can use {product} instead.
as routes are pattern matches, and the first match wins, you just need to put the in the correct order.
The former will force me to loose all the meaningful treelike pattern... how can i achieve this?
In mvc, the url will be reflected to controller and action anyway. But you can custom your url:
First way is to use attribute routing, add Route attribute to change the url displayed, don't forget to add
routes.MapMvcAttributeRoutes(); in RegisterRoutes().
[RoutePrefix("en/Category")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Route("SubCategory")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
} }
Second way is to change the MapRoute in RegisterRoutes(), so when enter en/...., the url will match the 'controller/action' in 'defaults'.
And be careful that this new MapRoute should be put above the default MapRoute:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "name1",
url: "en/{category}/{subcategory}/{product}",
defaults: new { controller = "Home", action = "Index", product = UrlParameter.Optional, category = UrlParameter.Optional, subcategory = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Participant
1226 Points
857 Posts
Custom routing
Oct 07, 2020 12:03 PM|0belix|LINK
Hi,
I'm used to working with WebForms, where i have been using Routing for some time now.
I'm considering migrating from WebForms to MVC, but while i was researching how to build complex routes, i'm left wondering how can i implement the same patterns that i have been implementing for years.
Here is an example of the routing models i need:
www.mywebsite.com/en/my-dynamic-category
www.mywebsite.com/en/my-dynamic-category/my-dynamic-sub-category
www.mywebsite.com/en/my-dynamic-category/my-dynamic-sub-category/my-dynamic-product
Note that i do not have a 'controller' name, nor an action... i use an url formatted unique name for each category, sub-category and product, so my routes would be something like:
www.mywebsite.com/{lang}/{category}/{subcategory}/{product}
Is there a way for me to follow these patterns, or is it unavoidable to change to a patter like:
www.mywebsite.com/{lang}/{controller}/{action}/{id}
resulting in URLs like
www.mywebsite.com/en/category/details/my-category
www.mywebsite.com/en/subcategory/details/my-subcategory
www.mywebsite.com/en/product/details/my-product
The former will force me to loose all the meaningful treelike pattern... how can i achieve this?
Thanks
All-Star
58114 Points
15633 Posts
Re: Custom routing
Oct 07, 2020 02:48 PM|bruce (sqlwork.com)|LINK
if you don't want the control and action names in the route url, then you just default them. {id} is a just a suggestion for the name of a action parameter, you can use {product} instead.
as routes are pattern matches, and the first match wins, you just need to put the in the correct order.
All-Star
48490 Points
18068 Posts
Re: Custom routing
Oct 07, 2020 04:41 PM|PatriceSc|LINK
Hi,
And what will be your login page? Controller and action is just common but routing is flexible enough to define where each url should be routed. See for example https://stackoverflow.com/questions/42414397/asp-net-core-mvc-catch-all-route-serve-static-file (I assume you'll go for ASP.NET Core ?) for a "catch all" route.
Note that at some point this is a logic problem ie you'll never be able to map a single /en/Home url to multiple destinations...
All-Star
58114 Points
15633 Posts
Re: Custom routing
Oct 07, 2020 09:55 PM|bruce (sqlwork.com)|LINK
that's easily handled by adding a login route rule before the general route.
Participant
940 Points
308 Posts
Re: Custom routing
Oct 08, 2020 05:46 AM|Jerry Cai|LINK
Hi,0belix
In mvc, the url will be reflected to controller and action anyway. But you can custom your url:
First way is to use attribute routing, add Route attribute to change the url displayed, don't forget to add routes.MapMvcAttributeRoutes(); in RegisterRoutes().
Second way is to change the MapRoute in RegisterRoutes(), so when enter en/...., the url will match the 'controller/action' in 'defaults'.
And be careful that this new MapRoute should be put above the default MapRoute:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "name1", url: "en/{category}/{subcategory}/{product}", defaults: new { controller = "Home", action = "Index", product = UrlParameter.Optional, category = UrlParameter.Optional, subcategory = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
And you can check these links:
https://www.tutorialsteacher.com/mvc/routing-in-mvc
https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/
Best Regards,
Jerry Cai