ASP.NET MVC4 doesn't load view in a different assembly

烈酒焚心 提交于 2019-12-08 13:51:30

Your views are not going to end up in the assembly unless you set each view file's 'Build Action' to "Embedded Resource" in Visual Studio (right-click the file, select properties). But then the views are still not going to be found by the runtime, since the default behavior is to look for views on disk, not inside assemblies. But this behavior can be changed.

So if you are really set on embedding the views inside your assembly, then you will have to implement a VirtualPathProvider and register it when the main application starts. All providers are given the chance to provide a 'virtual file', so your provider will get a chance to stream the view file out of the assembly (if and when the view is requested by the runtime).

An alternative is to add a post build event to your secondary project, that simply copies everything in the Views folder to the main project. In that case, I would advice you to use MVC areas, to prevent any conflicts when files are copied. This means you don't have to remember to embed view files, and you don't have to write the virtual path provider.

Use can use RazorGenerator in order to have (pre-compiled) views in different assemblies. Have a look at Usage in a View Library to see how.

Basically, the trick is to then consume the RazorGenerator.Mvc NuGet which will register a view engine and do the VirtualPathProvider stuff for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!