如何包括从外部项目进入MVC6控制器和视图? [英] How to include controllers and views from an external project into MVC6?

查看:349
本文介绍了如何包括从外部项目进入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 /浏览/首页/ TestView.cshtml
  〜/../ Module1.Web /浏览/首页/ TestView.cshtml
  /Module1.Web/Views/Home/TestView.cshtml
  /../ Module1.Web /浏览/首页/ 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不看的Web应用程序的根路径之外,并决定尝试实现我自己的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

    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/**"

下面是一个你可以复制并运行,看看它在行动的实现:
<一href=\"https://github.com/johnnyoshika/mvc6-view-components\">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控制器和视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆