I'm writing an MVC route and i was wondering, can i use something like an OR operator in it?

Something like this, where my Urls can either start with "shop/" or with "articles/"?

        routes.MapRoute(
            name: "Default",
            url: "shop|articles/{action}/{id}",
            defaults: new { controller = "Store", action = "Index", id = UrlParameter.Optional }
        );
有帮助吗?

解决方案

You could use a route constraint where you specify the valid values separated by pipes.

routes.MapRoute(
   name: "Default",
   url: "{page}/{action}/{id}",
   defaults: new { controller = "Store", action = "Index", id = UrlParameter.Optional },
   constraints: new { page = "shop|articles" });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top