.net mvc 路由 URL
.net mvc routing URL
我的 RouteConfig.cs
里有这个
routes.MapRoute(
name: "client1",
url: "client1/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "client2",
url: "client2/{controller}/{action}",/* updated parent folder to match route name */
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是我的看法。
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
当我 运行 我的应用程序 运行 在 http://localhost:50415/
但是 ActionLinks 呈现为:
{root}/client1
{root}/client1/Home/About
{root}/client1/Home/Contact
我的期望是动作链接呈现如下:
{root}
{root}/Home/About
{root}/Home/Contact
那么,当我从 http://localhost:50415/ 运行 宁时,为什么要为我的链接插入 client1 ??
我做错了什么?
MVC中的路由规则是按顺序处理的。第一个规则是最先匹配的规则。要解决这个问题,只需重新排列代码中的规则。您还可以使用约束将 "client" 规则折叠成一个规则:
routes.MapRoute(
name: "client",
url: "{client}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { client = "client1|client2" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我的 RouteConfig.cs
里有这个routes.MapRoute(
name: "client1",
url: "client1/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "client2",
url: "client2/{controller}/{action}",/* updated parent folder to match route name */
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是我的看法。
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
当我 运行 我的应用程序 运行 在 http://localhost:50415/
但是 ActionLinks 呈现为:
{root}/client1
{root}/client1/Home/About
{root}/client1/Home/Contact
我的期望是动作链接呈现如下:
{root}
{root}/Home/About
{root}/Home/Contact
那么,当我从 http://localhost:50415/ 运行 宁时,为什么要为我的链接插入 client1 ??
我做错了什么?
MVC中的路由规则是按顺序处理的。第一个规则是最先匹配的规则。要解决这个问题,只需重新排列代码中的规则。您还可以使用约束将 "client" 规则折叠成一个规则:
routes.MapRoute(
name: "client",
url: "{client}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { client = "client1|client2" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);