How to Load Views from Assembly in MVC

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

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.

ViewFile.cs

 

ViewPathProvider.cs

 

ViewCacheDependency.cs

 

ViewCacheDependencyManager.cs

 

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

The following two tabs change content below.

Saad Khan

Software Engineer at Falafel Software
Saad is an ASP.NET developer with 4 years of experience, and has also made a number of contributions to the Sitefinity marketplace and community. He loves to explore new code, and create innovative tools and procedures while exploring new approaches in his work with Sitefinity and MVC. As an experienced .NET developer, he frequents both Telerik and Microsoft technologies. In his spare time, Saad is an avid gamer and movie fanatic, and also experiments with new and interesting tools on the Microsoft platform.​​

Latest posts by Saad Khan (see all)

  • ALI ALIKHANI

    Hi
    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 http://1drv.ms/1GLK2hO. 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.