How to Load Views from Assembly in MVC

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

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.

HostingEnvironment.RegisterVirtualPathProvider(new ViewPathProvider());

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)


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