[英]tag helpers is wrong route link using net core 3.1 LTS
I use ASP.NET Core 3.1 LTS and route setting like this我使用 ASP.NET Core 3.1 LTS 和这样的路由设置
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Backend",
pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapRazorPages();
});
and my areas razor page have a tag helper我的区域剃刀页面有一个标签助手
<a class="btn btn-primary btn-sm" asp-area="Backend" asp-controller="Article" asp-action="Create"> Create</a>
usually this link will be /backend/article/create
通常这个链接是/backend/article/create
but result is /article/create?area=backend
但结果是/article/create?area=backend
why result is wrong ?为什么结果是错误的?
The registering order of routes matters.路由的注册顺序很重要。
\napp.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>\n{ {\n // register this route before the default one // 在默认路由之前注册此路由\n endpoints.MapControllerRoute( endpoints.MapControllerRoute(\n name: "Backend", name: "后端",\n pattern: "{area:exists}/{controller=Home}/{action=Index}");模式:“{area:exists}/{controller=Home}/{action=Index}”);\n endpoints.MapControllerRoute( endpoints.MapControllerRoute(\n name: "default",名称:“默认”,\n pattern: "{controller=Home}/{action=Index}/{id?}");模式:“{controller=Home}/{action=Index}/{id?}”);\n endpoints.MapRazorPages();端点.MapRazorPages();\n}); });\n
Make sure the area
of Backend
exists.确保Backend
area
存在。 Otherwise, it will be treated as querystring.否则,它将被视为查询字符串。 For example, if you have already had an Area
of Backend
(Need to use the standard Area Folder Structure + Namespace , and annotate the controller with [Area("Backend")]
, see official docs )例如,如果你已经有一个Area
of Backend
(需要使用标准的Area Folder Structure + Namespace ,并用[Area("Backend")]
注释控制器,请参阅官方文档)
[Area("Backend")] // important public class ArticleController : Controller { public IActionResult Create(){ ...}
then the following code然后下面的代码
<a asp-area="Backend" asp-controller="Article" asp-action="Create"> Create</a> <a asp-area="AnAreaThatDoesnotExist" asp-controller="Article" asp-action="Create"> Create</a>
will generate routes as below:将生成如下路线:
<a href="/Backend/Article/Create"> Create</a> <a href="/Article/Create?area=AnAreaThatDoesnotExist"> Create</a>
Note we specify an parameter of area=AnAreaThatDoesnotExist
which doesn't exist at all and then the area=AnAreaThatDoesnotExist
will be used as query string.请注意,我们指定了一个根本不存在的area=AnAreaThatDoesnotExist
参数,然后area=AnAreaThatDoesnotExist
将用作查询字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.