ASP.NET Core ViewLocationExpander 在其他程序集中找不到视图 [英] ASP.NET Core ViewLocationExpander can not find view in other assembly
问题描述
我有以下解决方案:
src
/Web
/Web.Admin
/Controller
/TestControllr.cs
/Views
/Test
/Index.cshtml
我将应用程序部分用于单独的我的应用程序部分:
I use Application Part for separate My application parts:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
var adminAssembly = Assembly.Load(new AssemblyName("Web.Admin"));
services.AddMvc().AddApplicationPart(adminAssembly);
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new AdminViewLocationExpander());
});
}
}
并创建一个名为AdminViewLocationExpander
的ViewLocationExpander:
and create a ViewLocationExpander name of AdminViewLocationExpander
:
public class AdminViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
const string adminAssembly = "Web.Admin";
var adminViewsLocations = new []
{
$"/{adminAssembly}/Views/{{1}}/{{0}}.cshtml",
$"/{adminAssembly}/Views/Shared/{{0}}.cshtml"
};
viewLocations = adminViewsLocations.Concat(viewLocations);
return viewLocations;
}
}
当我使用这个 URL localhost:6046/Test/Index
运行应用程序时,得到以下内容:
When I run the application with this URL localhost:6046/Test/Index
get following:
InvalidOperationException: The view 'Index' was not found. The following
locations were searched:
/Web.Admin/Views/Test/Index.cshtml
/Web.Admin/Views/Shared/Index.cshtml
/Views/Test/Index.cshtml
/Views/Shared/Index.cshtml
我该如何解决这个问题?
How Can I Solve this?
推荐答案
在我的项目中,我将视图作为嵌入资源保存在单独的类库项目/程序集中,打包为 nuget.例如 这里是一个带有嵌入式的项目视图.
In my projects I keep views as embedded resources in separate class library projects/assembly, packaged as nugets. For example here is one project with embedded views.
我不需要自定义 IViewLocationExpander 来找到它们.相反,我创建了一个这样的扩展方法:
I don't need a custom IViewLocationExpander to find them. Instead I create an extension method like this:
public static RazorViewEngineOptions AddCloudscribeSimpleContentBootstrap3Views(this RazorViewEngineOptions options)
{
options.FileProviders.Add(new EmbeddedFileProvider(
typeof(Bootstrap3).GetTypeInfo().Assembly,
"cloudscribe.SimpleContent.Web.Views.Bootstrap3"
));
return options;
}
然后在主应用程序的启动中,我像这样添加它们:
then in startup of the main app I add them like this:
services.AddMvc()
.AddRazorOptions(options =>
{
options.AddCloudscribeSimpleContentBootstrap3Views();
});
在带有嵌入视图的类库中,我只是像往常一样将它们放在 Views 文件夹中,而在我的 .csproj 文件中,我将它们嵌入如下资源:
In my class library with embedded views I just have them in a Views folder as normal, and in my .csproj file I make them embedded resources like this:
<ItemGroup>
<EmbeddedResource Include="Views\**" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
</ItemGroup>
它们的工作方式与在磁盘上一样,如果我想覆盖/自定义视图,我可以将其复制到本地,并且将使用本地视图而不是自动嵌入的视图.
They work the same as if they were on disk, and if I want to override/customize a view I can copy it locally and the local view will be used instead of the embedded one automatically.
这篇关于ASP.NET Core ViewLocationExpander 在其他程序集中找不到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!