Yve*_*ves 6 c# asp.net asp.net-core-mvc
我有一些模块,有控制器和视图.它基本上是我的Web应用程序的扩展.每个模块都在一个类库中.
我想从我的Web应用程序加载这些程序集.但我在这里没有运气.
我的解决方案文件结构如下:
src
|
|-- Web.Common (Class Library Project)
| |- Files like: filters, my own controller etc...
|
|-- WebApplication (ASP.NET5 WebSite)
| |- wwwroot
| |- Controllers
| |- Views
| |- etc...
|
|-- Module 1 (Class Library Project)
| |- Controllers
| |- Views
|
|-- Module 2 etc...
Run Code Online (Sandbox Code Playgroud)
这些是我试过的:
我试着实现自己的 IViewLocationExpander
public class CustomViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
yield return "/../Module1.Web/Views/Home/TestView.cshtml";
yield return "../Module1.Web/Views/Home/TestView.cshtml";
yield return "/Module1.Web/Views/Home/TestView.cshtml";
yield return "~/../Module1.Web/Views/Home/TestView.cshtml";
}
public void PopulateValues(ViewLocationExpanderContext context)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了各种各样的路径,但我没有运气:(
我明白了:
InvalidOperationException:找不到视图'TestView'.搜索了以下位置:
〜/ Module1.Web/Views/Home/TestView.cshtml~ /../ Module1.Web/Views/Home/TestView.cshtml /Module1.Web/Views/Home/TestView.cshtml /../Module1.Web/Views /Home/TestView.cshtml
所以我想也许默认的IFileProvider看起来不在WebApp的根路径之外,并决定尝试实现我自己的IFileProvider.
但在这里我也没有任何成功.
也许有一个功能是通过调用一些ASP.NET方法来实现这一点,但我不知道.
有什么建议?
控制器将自动加载.要加载视图,您将需要EmbeddedFileProvider和CompositeFileProvider,这两者都是新的,因此您需要从aspnetvnextFeed中获取它们.
在你的启动MVC6项目中引用它们project.json:
"Microsoft.AspNet.FileProviders.Composite": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*",
Run Code Online (Sandbox Code Playgroud)
更新您的服务注册Startup.cs:
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProvider = new CompositeFileProvider(
new EmbeddedFileProvider(
typeof(BooksController).GetTypeInfo().Assembly,
"BookStore.Portal" // your external assembly's base namespace
),
options.FileProvider
);
});
Run Code Online (Sandbox Code Playgroud)
在project.json外部程序集中,添加以下内容:
"resource": "Views/**"
Run Code Online (Sandbox Code Playgroud)
这是一个示例实现,您可以克隆并运行它以查看它的实际操作:https: //github.com/johnnyoshika/mvc6-view-components
| 归档时间: |
|
| 查看次数: |
4407 次 |
| 最近记录: |