我有一個Area
命名項目我試圖導航到。該區域有兩個名爲Project和Dashboard的Controller
,每個都有一個Index操作。使用ASP NET Core MVC 6錨標籤助手到區域控制器
[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?}");
...
});
當我在儀表板上輸入網址「http://localhost:1234:Projects/Dashboard」時,它的工作原理 - 顯示視圖。然而,其在此視圖中的項目控制器定義了以下標籤幫手,我得到的網址http://localhost:1234/Project?area=Projects
有沒有發現一個404:
<a asp-area="Projects" asp-controller="Project" asp-action="Index">
<span>Goto Project</span>
</a>
URL需要是http://localhost:1234/Projects/Project。如果我在瀏覽器地址欄中輸入它,則會顯示「項目索引視圖」。我假設我的路線配置正確。
我試過asp-area =「Projects」,並且省略了asp區域標籤。我多次瀏覽了area docs和routing 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
如何從儀表板導航使用錨標記輔助程序的項目?
有趣 - 我會更新問題。我也有這個問題,我不能在這個項目中使用以下技術添加控制器/視圖 - http://stackoverflow.com/questions/43291464/vs2017-asp-net-core-error-when-adding-a-razor - 查看 – Andez
一場你的代碼和我的區別的遊戲。這似乎是路線的順序。首先添加區域 - 在默認路線之前。 – Andez