How to include controllers and views from an external project into MVC6?(如何将外部项目中的控制器和视图包含到 MVC6 中?)
问题描述
我有一些具有控制器和视图的模块.它基本上是我的 Web 应用程序的扩展.每个模块都在一个类库中.
I have some modules which has controllers and views. It is basically an extension for my web application. Each module is in a class library.
我想从我的 Web 应用程序中加载这些程序集.但我在这里没有运气.
I want to load these assemblies from my web application. But I'm without luck here.
我的解决方案文件结构如下:
My solutions file structure is like:
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...
这些是我尝试过的:
我尝试实现自己的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)
{
}
}
我尝试了各种我想到的路径,但没有运气:(
I tried all kind of paths that came to my mind but no luck :(
我明白了:
InvalidOperationException:未找到视图TestView".搜索了以下位置:
InvalidOperationException: The view 'TestView' was not found. The following locations were searched:
~/Module1.Web/Views/Home/TestView.cshtml~/../Module1.Web/Views/Home/TestView.cshtml/Module1.Web/Views/Home/TestView.cshtml/../Module1.Web/Views/Home/TestView.cshtml
~/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.
So I thought maybe the default IFileProvider doesn't look outside the WebApp's root path and decided to try implementing my own IFileProvider.
但在这里我也没有任何成功.
But here I didn't have any success neither.
也许有一个功能可以通过调用一些 ASP.NET 方法来实现这一点,但我不知道.
Maybe there is a feature to achieve this by calling some ASP.NET methods but I don't know it.
有什么建议吗?
推荐答案
控制器会自动加载.要加载视图,您将需要 EmbeddedFileProvider
和 CompositeFileProvider
,它们都是新的,因此您需要从 aspnetvnext
提要中获取它们.
Controllers will get loaded automatically. To load views, you will need EmbeddedFileProvider
and CompositeFileProvider
, both of which are new, so you'll need to get them from the aspnetvnext
feed.
在您的启动 MVC6 项目的 project.json
中引用它们:
Reference them in your startup MVC6 project's project.json
:
"Microsoft.AspNet.FileProviders.Composite": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*",
在 Startup.cs
中更新您的服务注册:
Update your service registration in 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
);
});
在您的外部程序集的 project.json
中,添加以下内容:
In project.json
of your external assembly, add this:
"resource": "Views/**"
这是一个示例实现,您可以克隆并运行它以查看它的实际效果:https://github.com/johnnyoshika/mvc6-view-components
Here's a sample implementation that you can clone and run to see it in action: https://github.com/johnnyoshika/mvc6-view-components
这篇关于如何将外部项目中的控制器和视图包含到 MVC6 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将外部项目中的控制器和视图包含到 MVC6 中?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30