MVC 5自定义视图引擎,用于加载外部控制器和视图

时间:2023-01-16 09:44:02

REQUIREMENT

要求

I have a project and one of requirements is load external small websites like plugins. This websites must accomplish few requirements of course (like mvc and net version and other more to make it compatible).

我有一个项目,其中一个要求是加载外部的小型网站,比如插件。当然,这些网站必须完成很少的需求(比如mvc和net版本,以及其他使其兼容的需求)。

This plugins must be allocated in a new folder inside my MVC Website. And in it there are Views folder and compiled website dll result (controllers, models,...)

这个插件必须在我的MVC网站的一个新文件夹中分配。其中有视图文件夹和已编译的网站dll结果(控制器、模型…)

CURRENT DEVELOPMENT

发展现状

I implementented a custom ViewEngine to load Views from this folder, and a ControllerFactory that makes the trick for controllers that are not located in my main website.

我实现了一个自定义的ViewEngine来从这个文件夹加载视图,以及一个ControllerFactory,它可以为不在我的主网站上的控制器提供诀窍。

I based my code in http://christianjvella.com/wordpress/mef-mvc-defining-controllerfactory/ for ControllerFactory and Can I specify a custom location to "search for views" in ASP.NET MVC? for ViewEngines

我的代码基于http://christianjvella.com/wordpress/mef-mvc- define -controllerfactory/ for ControllerFactory,我可以指定一个自定义位置在ASP中“搜索视图”。净MVC吗?对于ViewEngines

PROBLEM

问题

All seems to works well with a simple html view. But when I use some MVC helpers or objects like ViewBag, @Url.Content, @Html.DropDownList i have an error that the object is null or not exist.

所有这些似乎都适用于简单的html视图。但是当我使用一些MVC帮助或者对象时,比如ViewBag, @Url。内容,@Html。下拉列表我有一个错误,该对象为空或不存在。

Overriding CreateView method in ViewEngine, it returns an IView instance correctly and controllerContext have ViewBag and other necessary features.

在ViewEngine中重写CreateView方法,它正确地返回IView实例,controllerContext有ViewBag和其他必要的特性。

It seems that, at the moment of view rendering, MVC doesn't attach controllerContext to view instance...

在视图呈现时,MVC似乎没有将controllerContext附加到view instance…

Is this a limitation of MVC that don't pass controllerContext to external loaded views or externals controllers? Any idea or workaround?

这是MVC的限制吗?任何想法或解决方案?

3 个解决方案

#1


2  

Your views have to be pre-compiled. You can use precompiled MVC views for doing so. Precompiled views are created using a VS Addin called as RazorGenerator.

您的视图必须预先编译。您可以使用预编译的MVC视图来实现这一点。预编译视图是使用一个名为RazorGenerator的VS插件创建的。

You can read more about here, below example puts Views in separate class library! ,some thing that you are looking for:

您可以在这里阅读更多,下面的示例将视图放在单独的类库中!,你正在寻找的东西:

http://www.c-sharpcorner.com/UploadFile/97fc7a/article-precompiled-razor-view-using-razorgenerator-mvc-and/

http://www.c-sharpcorner.com/UploadFile/97fc7a/article-precompiled-razor-view-using-razorgenerator-mvc-and/

#2


0  

Well, I forget to include web.config in plugins Views folder, with it works fine because it defines Razor and MVC dll references.

嗯,我忘了包括网络。配置在插件视图文件夹中,它工作得很好,因为它定义了Razor和MVC dll引用。

Thanks for all

谢谢你的所有

#3


0  

You could also use a VirtualPathProvider to serve things from other assemblies, including static assets and views. This way you can use the standard ViewENgine

您还可以使用VirtualPathProvider来服务其他程序集,包括静态资产和视图。通过这种方式,您可以使用标准的ViewENgine

https://www.nuget.org/packages/EmbeddedResourceVirtualPathProvider/

https://www.nuget.org/packages/EmbeddedResourceVirtualPathProvider/

#1


2  

Your views have to be pre-compiled. You can use precompiled MVC views for doing so. Precompiled views are created using a VS Addin called as RazorGenerator.

您的视图必须预先编译。您可以使用预编译的MVC视图来实现这一点。预编译视图是使用一个名为RazorGenerator的VS插件创建的。

You can read more about here, below example puts Views in separate class library! ,some thing that you are looking for:

您可以在这里阅读更多,下面的示例将视图放在单独的类库中!,你正在寻找的东西:

http://www.c-sharpcorner.com/UploadFile/97fc7a/article-precompiled-razor-view-using-razorgenerator-mvc-and/

http://www.c-sharpcorner.com/UploadFile/97fc7a/article-precompiled-razor-view-using-razorgenerator-mvc-and/

#2


0  

Well, I forget to include web.config in plugins Views folder, with it works fine because it defines Razor and MVC dll references.

嗯,我忘了包括网络。配置在插件视图文件夹中,它工作得很好,因为它定义了Razor和MVC dll引用。

Thanks for all

谢谢你的所有

#3


0  

You could also use a VirtualPathProvider to serve things from other assemblies, including static assets and views. This way you can use the standard ViewENgine

您还可以使用VirtualPathProvider来服务其他程序集,包括静态资产和视图。通过这种方式,您可以使用标准的ViewENgine

https://www.nuget.org/packages/EmbeddedResourceVirtualPathProvider/

https://www.nuget.org/packages/EmbeddedResourceVirtualPathProvider/