InvalidOperationException:未找到视图
InvalidOperationException: The view was not found
我有一个 ASP.Net 项目,我想使用 class 库项目来创建可重用的 Razor 视图组件。我添加了 AspNetCore.Mvc
和 AspNetCore.Razor.Tools
对我的 class 库项目的引用。
我在 Components/DataTableComponent.cs
文件中创建了 ViewComponent
class:
namespace Rasha.Utility.Components
{
public class DataTableComponent:ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
var model = new DataTableModel()
{
Name = "Alliiiiiiiiiiiiiiiii"
};
return View("Default.cshtml", model);
}
}
}
并在 Views/Shared/Components/DataTableComponent/Default.cshtml
文件中查看。
这是我的 class 库 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Tools" Version="1.1.0-preview4-final" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\Shared\Components\**\*.cshtml" />
</ItemGroup>
</Project>
为什么我会收到此错误:
InvalidOperationException: The view 'Components/DataTableComponent/Default.cshtml' was not found. The following locations were searched:
/Areas/Administration/Views/Customer/Default.cshtml
编译器似乎在我的 MVC 项目而不是 class 库项目中寻找视图。
谢谢。
我的梦想成真了:
Creating a Razor Class Library project in ASP.NET Core 现已上市。
我有一个 ASP.Net 项目,我想使用 class 库项目来创建可重用的 Razor 视图组件。我添加了 AspNetCore.Mvc
和 AspNetCore.Razor.Tools
对我的 class 库项目的引用。
我在 Components/DataTableComponent.cs
文件中创建了 ViewComponent
class:
namespace Rasha.Utility.Components
{
public class DataTableComponent:ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
var model = new DataTableModel()
{
Name = "Alliiiiiiiiiiiiiiiii"
};
return View("Default.cshtml", model);
}
}
}
并在 Views/Shared/Components/DataTableComponent/Default.cshtml
文件中查看。
这是我的 class 库 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Tools" Version="1.1.0-preview4-final" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\Shared\Components\**\*.cshtml" />
</ItemGroup>
</Project>
为什么我会收到此错误:
InvalidOperationException: The view 'Components/DataTableComponent/Default.cshtml' was not found. The following locations were searched: /Areas/Administration/Views/Customer/Default.cshtml
编译器似乎在我的 MVC 项目而不是 class 库项目中寻找视图。
谢谢。
我的梦想成真了:
Creating a Razor Class Library project in ASP.NET Core 现已上市。