tag-helpers无法正确生成我的网址

Dav*_*ave 5 asp.net asp.net-mvc razor asp.net-core-mvc

我设置了以下路线

    app.UseMvc(routes =>
    {

        routes.MapRoute(
            name: "admin",
            template: "{controller=Home}/{action=Index}/{id?}",
            defaults: new {Area = "Admin"},
            constraints: new {HostConstraint = new MyConstraint()});
        routes.MapRoute(
            name: "admin-rep",
            template: "Rep/{controller=Home}/{action=Index}/{id?}",
            defaults: new { Area = "" },
            constraints: new { HostConstraint = new MyConstraint() });

        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
Run Code Online (Sandbox Code Playgroud)

在这种情况下,MyConstraint总是返回true.

  • 如果我尝试访问路径"/",我最终会进入区域管理员,控制器主页,操作索引.没关系.
  • 如果我尝试访问"/ rep",那么我最终会进入根区域,控制器主页,动作索引.好 .问题是该视图有一个链接引用我的HomeController中的另一个动作(来自根区域).我期待链接指向/ rep/home/action2,但生成的路由是/ home/action2.这就像生成网址时有一些错误的匹配?
<a asp-action="Action">Action</a>

@Html.ActionLink("Action", "Action")
Run Code Online (Sandbox Code Playgroud)

Ali*_*ani 1

您使用过Area,但我认为您没有asp-area在标签助手中指定属性,这导致了这个问题。有关设置的更多信息,asp-area请参阅下面的链接:

Asp.Net MVC Core 1.0 - 带有空白区域的锚标记助手