【问题标题】:Using ASP NET Core MVC 6 Anchor Tag Helper to an Area Controller将 ASP NET Core MVC 6 Anchor Tag Helper 用于区域控制器
【发布时间】:2017-04-08 12:42:58
【问题描述】:

我有一个Area,命名为我正在尝试导航到的项目。该区域有两个Controllers,分别名为 Project 和 Dashboard,每个都有一个 Index 操作。

[Authorize]
[Area("Projects")]
public class ProjectController : Controller
{
    ...
}

[Authorize]
[Area("Projects")]
public class DashboardController : Controller
{
    ...
}

我在 Startup.cs 中定义了以下路由。

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapRoute(
        name: "Projects",
        template: "{area}/{controller}/{action}/{id?}");

    ...
});

当我在仪表板中输入 url “http://localhost:1234:Projects/Dashboard” 时,它可以工作 - 显示视图。但是,在此视图中为 Project 控制器定义了以下标签助手,我得到了 url http://localhost:1234/Project?area=Projects 并没有找到 404:

<a asp-area="Projects" asp-controller="Project" asp-action="Index">
    <span>Goto Project</span>
</a>

网址必须是http://localhost:1234/Projects/Project。如果我在浏览器地址栏中输入它,那么它会显示项目索引视图。我假设我的路线配置正确。

我已经尝试过 asp-area="Projects" 并且也省略了 asp-area 标签。我已经多次搜索area docsrouting docs 无济于事。

我的目标是 .NET Framework 4.6.2。以下是 csproj 感兴趣的领域:

<PropertyGroup>
    <TargetFramework>net462</TargetFramework>
    <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x86</PlatformTarget>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" PrivateAssets="All" />
    <PackageReference Include="SimpleInjector.Integration.AspNetCore.Mvc" Version="3.3.2" />
    <PackageReference Include="Telerik.UI.for.AspNet.Core" Version="2017.1.223" />
</ItemGroup>
<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>

我在项目的根级别创建了一个名为 Areas 的文件夹。即:

ACMEMVCWebApp\Areas\Projects
ACMEMVCWebApp\Areas\Projects\Controllers
ACMEMVCWebApp\Areas\Projects\Views

如何使用 Anchor Tag Helper 从仪表板导航到项目?

【问题讨论】:

    标签: asp.net asp.net-core-mvc


    【解决方案1】:

    我刚刚尝试了一个新的应用程序,它工作正常。

    您使用的是哪个版本的 .Net Core?

    我只是检查了implementation,应该没问题。可能是一些包不匹配?

    这是我的示例配置。我将区域路线放在顶部,如下所示:

    【讨论】:

    • 有趣 - 我会更新问题。我还有一个问题,我无法在这个项目中使用以下技术添加控制器/视图 - stackoverflow.com/questions/43291464/…
    • 一个找出你的代码和我的代码之间差异的游戏。这似乎是路线的顺序。首先添加区域 - 在默认路线之前。
    【解决方案2】:

    在为区域内的控制器操作生成正确的 URL 时,我也遇到了这个问题。我找到了一些有相同问题的人可能感兴趣的修复程序。

    路由的顺序是为操作创建正确 url 的问题。在我的例子中,我正在为表单生成操作,并将一些代码从 MVC 5 移动到 MVC Core 2.0。原始代码使用了这样的 html 助手:

    @using (Html.BeginForm())
    

    并产生了以下正确的标记:

    <form action="/Admin/Role/Create?_=1505204033132" method="post">
    

    然后我更新了代码以使用标签助手,因为我非常喜欢这种语法。我把上面的改成:

    <form asp-area="Admin" asp-controller="Role" asp-action="Create" method="post">
    

    这没有创建正确的 url。它生成的代码如下所示:

    <form method="post" action="/Role/Create?area=Admin">
    

    使用此 url 会得到预期的 404 响应。

    在 startup.cs 中更改周围路由的顺序,以便首先修复区域路由问题并且现在 url 正确。

    我不知道为什么会这样,并且非常惊讶地发现路由可以控制 url 的生成。这当然有能力再次抓住我,因为我会认为这两者是独立的实体,并且路由仅在发出请求时使用 - 似乎这不是一个正确的假设。

    戈登

    【讨论】:

      【解决方案3】:

      引用documentation

      Routes in the route collection are ordered, and will be processed in the order they are added. So in this example, the blog route will be tried before the default route.

      所以这需要在默认路线之前先是区域:

      app.UseMvc(routes =>
      {
          routes.MapRoute(
              name: "Projects",
              template: "{area}/{controller}/{action}/{id?}");
      
          routes.MapRoute(
              name: "default",
              template: "{controller=Home}/{action=Index}/{id?}");
      
      }
      

      鉴于此,我仍然觉得直接输入网址很奇怪。问题出在 Tag Helpers 中。

      【讨论】:

      • 嗯,不知何故,url 生成 ( Generator.GenerateActionLink ) 不关心订单或以不同方式使用它。我值得向 MVC repo 提出问题以获得正确的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2014-12-02
      • 2021-03-22
      • 2017-08-30
      • 2018-02-13
      • 2017-11-16
      • 1970-01-01
      相关资源
      最近更新 更多