Skip to main content
 首页 » 编程设计

c#之MVC URL 模式中的 OR 运算符

2024年02月13日18pengyingh

我正在编写一个 MVC 路由,我想知道,我可以在其中使用类似 OR 运算符的东西吗?

像这样,我的 Urls 可以以“shop/”或“articles/”开头吗?

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

请您参考如下方法:

您可以使用路由约束,在其中指定由竖线分隔的有效值。

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