Asp.net mvc 如何进行这些类型的MVC路由?
我登记了两条路线:Asp.net mvc 如何进行这些类型的MVC路由?,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我登记了两条路线: //News page routing with optional news article ID routes.MapRoute( name: "News", url: "news/{id}", defaults: new { controller = "News", action = "Index", id = UrlParameter.Optional } ); 默认链接(无id)生成为:@Html.ActionLink(“新闻”、“索引”、“新闻”) 及 链接生成为
//News page routing with optional news article ID
routes.MapRoute(
name: "News",
url: "news/{id}",
defaults: new { controller = "News", action = "Index", id = UrlParameter.Optional }
);
默认链接(无id)生成为:@Html.ActionLink(“新闻”、“索引”、“新闻”)
及
链接生成为:
[a href="@Url.Action("Index", "Account", new RouteValueDictionary(new { id = "test-user-id" }))"]Me[/a]
到目前为止一切都很好,我的新闻链接创建为domain/News并起作用,我的链接创建为domain/test用户id并起作用
但是一旦我访问了Me链接,新闻链接就变成了domain/News/test用户id,这不是我想要的。如何使用mvc路由“分离”新闻id和用户id???在新闻链接中,您的url首先具有控制器,然后是操作 您应该在Me链接中遵循相同的模式。i、 e
{controller}/{action}/{id}在新闻链接中,您的url首先拥有控制器,然后是操作 您应该在Me链接中遵循相同的模式。i、 e
{controller}/{action}/{id}我不太清楚为什么会发生这种情况,但我对路由有一些建议。首先,对于用户路由,删除
UrlParameter.Optional
,因为它实际上不是可选的。另外,从URL模板中删除{controller}
和{action}
,因为它们不是必需的
//User page routing with User ID
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Account", action = "Index" }
);
其次,对概要文件链接使用@Url.RouteUrl
而不是@Url.Action
,因为这更准确地针对路由规则
新闻:
@Html.RouteLink("News", "News")
@Url.RouteUrl("Profile", new { id = "test-user-id" })
配置文件:
@Html.RouteLink("News", "News")
@Url.RouteUrl("Profile", new { id = "test-user-id" })
我不太清楚为什么会发生这种情况,但我对路由有一些建议。首先,对于用户路由,删除
UrlParameter.Optional
,因为它实际上不是可选的。另外,从URL模板中删除{controller}
和{action}
,因为它们不是必需的
//User page routing with User ID
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Account", action = "Index" }
);
其次,对概要文件链接使用@Url.RouteUrl
而不是@Url.Action
,因为这更准确地针对路由规则
新闻:
@Html.RouteLink("News", "News")
@Url.RouteUrl("Profile", new { id = "test-user-id" })
配置文件:
@Html.RouteLink("News", "News")
@Url.RouteUrl("Profile", new { id = "test-user-id" })