1. <legend id='npMXc'><style id='npMXc'><dir id='npMXc'><q id='npMXc'></q></dir></style></legend>
        <bdo id='npMXc'></bdo><ul id='npMXc'></ul>
    2. <small id='npMXc'></small><noframes id='npMXc'>

      <tfoot id='npMXc'></tfoot>
      <i id='npMXc'><tr id='npMXc'><dt id='npMXc'><q id='npMXc'><span id='npMXc'><b id='npMXc'><form id='npMXc'><ins id='npMXc'></ins><ul id='npMXc'></ul><sub id='npMXc'></sub></form><legend id='npMXc'></legend><bdo id='npMXc'><pre id='npMXc'><center id='npMXc'></center></pre></bdo></b><th id='npMXc'></th></span></q></dt></tr></i><div id='npMXc'><tfoot id='npMXc'></tfoot><dl id='npMXc'><fieldset id='npMXc'></fieldset></dl></div>

      如何将外部项目中的控制器和视图包含到 MVC6 中?

      时间:2023-07-10
    3. <legend id='KvUwK'><style id='KvUwK'><dir id='KvUwK'><q id='KvUwK'></q></dir></style></legend>

        <small id='KvUwK'></small><noframes id='KvUwK'>

          <bdo id='KvUwK'></bdo><ul id='KvUwK'></ul>

        • <i id='KvUwK'><tr id='KvUwK'><dt id='KvUwK'><q id='KvUwK'><span id='KvUwK'><b id='KvUwK'><form id='KvUwK'><ins id='KvUwK'></ins><ul id='KvUwK'></ul><sub id='KvUwK'></sub></form><legend id='KvUwK'></legend><bdo id='KvUwK'><pre id='KvUwK'><center id='KvUwK'></center></pre></bdo></b><th id='KvUwK'></th></span></q></dt></tr></i><div id='KvUwK'><tfoot id='KvUwK'></tfoot><dl id='KvUwK'><fieldset id='KvUwK'></fieldset></dl></div>

                <tfoot id='KvUwK'></tfoot>
                  <tbody id='KvUwK'></tbody>
                本文介绍了如何将外部项目中的控制器和视图包含到 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.

                有什么建议吗?

                推荐答案

                控制器会自动加载.要加载视图,您将需要 EmbeddedFileProviderCompositeFileProvider,它们都是新的,因此您需要从 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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:asp.net core mvc 密码验证器 下一篇:ASP.NET Core MVC(前 MVC 6)Razor 错误未被异常过滤器捕获

                相关文章

                  <bdo id='RgGvR'></bdo><ul id='RgGvR'></ul>

              1. <tfoot id='RgGvR'></tfoot>

                1. <i id='RgGvR'><tr id='RgGvR'><dt id='RgGvR'><q id='RgGvR'><span id='RgGvR'><b id='RgGvR'><form id='RgGvR'><ins id='RgGvR'></ins><ul id='RgGvR'></ul><sub id='RgGvR'></sub></form><legend id='RgGvR'></legend><bdo id='RgGvR'><pre id='RgGvR'><center id='RgGvR'></center></pre></bdo></b><th id='RgGvR'></th></span></q></dt></tr></i><div id='RgGvR'><tfoot id='RgGvR'></tfoot><dl id='RgGvR'><fieldset id='RgGvR'></fieldset></dl></div>

                  1. <small id='RgGvR'></small><noframes id='RgGvR'>

                    <legend id='RgGvR'><style id='RgGvR'><dir id='RgGvR'><q id='RgGvR'></q></dir></style></legend>