我试图添加路线但没有成功。 我有默认的溃败。服务器网址如http://server.com/Controller/Action(URL1) 但我还需要添加此网址:http://server.com/BlueTheme/Controller/Action(URL2) 我正在检查请求中的参数并为用户添加特殊主题。 我知道路由配置顺序如何工作,所以在我配置的下面,但是当使用URL1时,看起来第一条路由工作。这很奇怪,请帮忙。
更新 Url.Action(" Action"," Controller")生成" BlueTheme / Controller / Action" (当我使用URL1时) 那是什么让我说'#34;它似乎是第一条路线工作"。有人知道为什么会这样,我该怎么办?
routes.MapRoute(
name: "Other Theme",
url: "BlueTheme/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", HttpRoute = true }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", HttpRoute = true }
);
答案 0 :(得分:1)
你的路线的顺序看起来很好,但我想你想要
routes.MapRoute(
name: "Other Theme",
url: "BlueTheme/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", theme = "BlueTheme", HttpRoute = true }
);
请注意,我添加了theme
路由值,然后您的应用程序的其余部分可以访问该值:
public ActionResult Index(string theme){ ... }
不是在网址中嵌入“BlueTheme”,而是使用约束可能会更优雅:
routes.MapRoute(
name: "Other Theme",
url: "{theme}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", theme = "BlueTheme", HttpRoute = true },
constraints: new { theme = "BlueTheme|BlackTheme|WhiteTheme" }
);