locked
Custom routing RRS feed

Answers

  • User1312693872 posted

    Hi,0belix

    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 }
                );
            }
        }

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:46 AM

All replies

  • User-474980206 posted

    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. 

    Wednesday, October 7, 2020 2:48 PM
  • User753101303 posted

    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...

    Wednesday, October 7, 2020 4:41 PM
  • User-474980206 posted

    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...

    that's easily handled by adding a login route rule before the general route.

    Wednesday, October 7, 2020 9:55 PM
  • User1312693872 posted

    Hi,0belix

    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 }
                );
            }
        }

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:46 AM