[英]Generating URLs for ASP.NET MVC Area routing with subfolder
I have an ASP.NET MVC app which has areas with special routes as following:我有一个 ASP.NET MVC 应用程序,它具有以下特殊路线的区域:
Area registration file:区域登记文件:
context.Routes.MapHttpRoute(
"AccessControlApi_default",
"accesscontrol/api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
context.MapRoute(
"AccessControl_dashboardwidgets",
"accesscontrol/dashboardwidgets/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "AccessControl.Controllers.DashboardWidgets" }
);
context.MapRoute(
"AccessControl_default",
"accesscontrol/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "AccessControl.Controllers" }
);
First routing is for api, second is for controllers and views in a sub-folder under the area, and third is for the default controllers under area.第一个路由是api,第二个是area下子文件夹中的controllers和views,第三个是area下的默认controllers。
Everything is working well with routing, but the issue is with the use of Url.Action
like this one:一切都在路由方面运行良好,但问题在于像这样使用
Url.Action
:
<a href='@Url.Action("Index","Home",new{area="AccessControl"})'>Go to home</a>
It's always injecting dashboardwidgets keyword in the url between area and controller like this: {host}/accesscontrol/ dashboardwidgets /Home/Index它总是在区域和控制器之间的 url 中注入dashboardwidgets关键字,如下所示: {host}/accesscontrol/ dashboardwidgets /Home/Index
How can I generate urls according to my needs, either to root of area, or to this sub folder under the area??如何根据我的需要生成url,要么是区域的根目录,要么是区域下的这个子文件夹??
I think the solution would be to use named routes in constructing your links.我认为解决方案是在构建链接时使用命名路由。 You would need to switch the call from
您需要将呼叫从
@Url.Action("Index","Home",new{area="AccessControl"})
to到
@Url.RouteUrl("AccessControl_dashboardwidgets", new {area = "AccessControl", controller="Home", action="Index"})
or或者
@Url.RouteUrl("AccessControl_default", new {area = "AccessControl", controller="Home", action="Index"})
Depending on which route you are aiming for.取决于您的目标路线。
Sorry about the confusion with the parameters, was editing while doing something else... multitasking on Monday morning is obviously to be avoided:)很抱歉参数混乱,正在做其他事情的同时进行编辑...显然要避免周一早上的多任务处理:)
为了使用@Url.Action 扩展方法,您需要为 routeValues 参数指定 httproute 名称,如下所示:
@Url.Action("Index","Home", new { httproute ="AccessControlApi_default"} )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.