[英]C# Custom Map Routes / View Paths / Link Generation
Issue/Try 1:问题/尝试 1:
I have a custom route map:我有一个自定义路线图:
routes.MapRoute(
name: "User Profile",
url: "User/{userId}/{controller}/{action}/{id}",
defaults: new { Areas = "User", controller = "Kpi", action = "Index", id = UrlParameter.Optional }
);
If i manually navigate to the URL /User/f339e768-fe92-4322-93ca-083c3d89328c/Kpi/View/1
the page loads with a View Error: The view 'View' or its master was not found or no view engine supports the searched locations
.如果我手动导航到 URL
/User/f339e768-fe92-4322-93ca-083c3d89328c/Kpi/View/1
页面加载一个视图错误: The view 'View' or its master was not found or no view engine supports the searched locations
。
Issue/Try 2:问题/尝试 2:
Stopped using the custom route and set up my controller as instead:停止使用自定义路由并将我的控制器设置为:
[RouteArea("User")]
[RoutePrefix("{userId}/Kpi")]
public class KpiController : BaseUserController
{
[Route("View/{id}")]
public async Task<ActionResult> View(string userId, int? id = null)
{
[...]
}
}
This now works i can navigate to the URL and the View displays fine.这现在有效,我可以导航到 URL 并且视图显示正常。
Issue for both:两者的问题:
Although I can navigate manually to both and they load I can't seem to generate the URL correctly using ActionLink
:尽管我可以手动导航到两者并加载它们,但我似乎无法使用
ActionLink
正确生成 URL:
@Html.ActionLink(kpi.GetFormattedId(), "View", "Kpi", new { Area = "User", userId = Model.Id, id = kpi.Id }, null)
It generates: /User/Kpi/View/1?userId=f339e768-fe92-4322-93ca-083c3d89328c
instead of /User/f339e768-fe92-4322-93ca-083c3d89328c/Kpi/View/1
它生成:
/User/Kpi/View/1?userId=f339e768-fe92-4322-93ca-083c3d89328c
而不是/User/f339e768-fe92-4322-93ca-083c3d89328c/Kpi/View/1
URL Mapping网址映射
After some time i have found the solution for the custom mapping i was adding in the main RouteConfig.cs
and not in the Area
Registration.一段时间后,我找到了自定义映射的解决方案,我将其添加到主
RouteConfig.cs
而不是Area
注册中。 Moving the MapRoute
to the Area
works correctly and without the RouteArea
, RoutePrefix
and Route
attributes in the Controller.将
MapRoute
移动到Area
可以正常工作,并且控制器中没有RouteArea
、 RoutePrefix
和Route
属性。
Area Registration区域登记
public class UserAreaRegistration : AreaRegistration
{
public override string AreaName => "User";
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "User",
url: "User/{userId}/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"User_default",
"User/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
}
Links链接
Instead of using ActionLink
i am now using RouteLink
.我现在使用
RouteLink
而不是使用ActionLink
。
@Html.RouteLink("KPIs", "User", new { Controller = "Kpi", Action = "Index", userId = Model.Id })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.