How to Load Views from Assembly in MVC

By April 7, 2014.NET, C#, Web

There can be situations when we require our pages to be loaded from database or assembly, instead of file system. One would want to hide the implementation and place all the resources in an assembly. In this post, I’ll show you how to do it in a very simplified way.

Step 1

Register your routing rule like this.

I have named my Controller and Action, Frontend and Default respectively. Also, defined a constraint that no other controller and / or action will be processed. One more thing you must have noticed is {*viewName}. This means that all URLs will invoke our Default action.

Step 2

I created 4 classes to do some magic, which are as follows.









Step 3

Register the ViewPathProvider class in Global.asax.


This will tell MVC engine to call this registered class for resources. And of course, this class should implement some basic requirements. MVC engine first calls FileExists method to check if the resource exist in file system – no, it’s assembly in our case. If the method returns true, it calls GetFile method to get the content as VirtualFile, which we have inherited in ViewFile class.

Step 4

Now create an helper class which will two methods as shown below.


Finally, you would need a controller, who’s one and only one action will always be called.


And guess what, you can download sample project from the link below. Happy coding!

Sample Project


    This Sample Is Awesome But This Not Work On IIS , IIS Say Cant Find View Files

  • Jose Incera

    Can I use several cultures in the external Assembly? How do I change the Neutral culture so I can change the language from the embedded View. Thanks

    • Saad Khan

      Hi Jose, I will try to work on a sample for you. I think this should be the part of “viewName” like en/home and ar/home. And views should be in the relative folders in assembly so that method GetByVirtualPath could find it.

      • Jose Incera

        Thank you for your anwser, well, im working with Resources Library for multilanguage. And I have two MVC projects. Im using the same multilanguage pattern for both . And they work great. The problem is when I tried to show a Embedded View, it ignore my CurrentCulture and always use EN-US witch is the default Languaje of the Embedded. I will ppreciate any help you can give me. Regards

        • Saad Khan

          Hi Jose, I prepared a sample project for you demonstrating how to load views with cultures all from an assembly. Here’s the link to the code Hope this helps you!

          • Jose Incera

            Thank you very much for your time, I ask you this beause i was having a problem. I was using a class library for resources, the problem was that I didnt make a copy to es resource, i tought that there were only one dll, but they will be sereveral.
            Again thank you for your time.

  • Paris Sequeira

    Hi there. I can load and see the views dynamically. However the views are not executing the validation rules (it looks like ignoring the Required Data Annotation). Besides… the views embedded in the external DLL has references to angular but those scripts are not executed neither

    • Paris Sequeira

      Just to clarify my question…

      External DLL

      Local Project
      1. Loads View from External.dll. That view is trying to execute some functions from Functions.Js but it is not executing them
      2. Loads View from External.dll. That view is trying to execute some functions from LocalFunctions.Js (which belongs to LocalProject) but it is not executing them