【发布时间】:2017-09-23 13:55:50
【问题描述】:
我有以下解决方案:
src
/Web
/Web.Admin
/Controller
/TestControllr.cs
/Views
/Test
/Index.cshtml
我将应用程序部分用于单独的我的应用程序部分:
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:
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 运行应用程序时,得到以下信息:
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
我该如何解决这个问题?
【问题讨论】:
-
你的项目被命名为“App.Web.Admin”而不是“Web.Admin”所以
adminAssembly变量应该设置为App.Web.Admin,对吧? -
@NickGeorge 不,我编辑问题
标签: c# asp.net-mvc razor asp.net-core