RSS

Change Shared or Views folder location in an MVC 3 site

01 Aug

if you want to change the location of the shared folder in your MVC 3 App, then this is a must read.

when you create a new MVC 3 Web Application Project, a default folder structure is created for you that looks something like this

01

to change the shared folder location you must do the following steps

Move the shared folder to the Root

just drag the shared folder out of the views folder to the root of your app, like so

02

Copy the web.config

in order for the shared folder to work outside the views folder, it has to have a special web.config file within it. just copy the one inside the views folder and paste it into the shared folder.

03

Edit the _ViewStart.vbhtml page

now that the _layout page has changed its locations, we have to modify the _ViewStart.vbhtml page located in the Views folder, and edit the following

04

remove the “/Views” so now it directs to the _layout page located in the new shared folder location.

05

Code a custom view engine

if you tried to browse the site now you will get an error like this one

06

that is happening because MVC is using the default locations for the partial views, so its still looking for the _LogOnPartial.vbhtml in its old location, but you moved that file to a new location in the root of your app.

to change the default file location for the whole site we need to create a new class that inherits from RazorViewEngine


Public Class MyCustomViewEngine
    Inherits RazorViewEngine

    Sub New()

        MasterLocationFormats = New String() {"~/Shared/{0}.vbhtml"}

        ViewLocationFormats = New String() {"~/Views/{1}/{0}.vbhtml", "~/Shared/{0}.vbhtml"}

        PartialViewLocationFormats = New String() {"~/Views/{1}/{0}.vbhtml", "~/Shared/{0}.vbhtml"}

    End Sub

End Class

 

here we set new locations for the Master, Views, and PartialViews

Replace the default view engine and test

all we have to do now is plugin this new custom engine, go to the Global.asax file and add the following to the Application_Start()


  Sub Application_Start()
        AreaRegistration.RegisterAllAreas()

        ViewEngines.Engines.Clear()
        ViewEngines.Engines.Add(New MyCustomViewEngine)

        RegisterGlobalFilters(GlobalFilters.Filters)
        RegisterRoutes(RouteTable.Routes)
    End Sub

now if you run the app you will notice that it all works with the shared folder new location.

Summary

we managed to change the shared folder location and replace the default razor view engine with our own implementation that changes the locations of the master, views, and partial views of the app.

Advertisements
 
2 Comments

Posted by on 1, Aug 2011 in .NET, ASP.NET, MVC, VB.NET

 

Tags: , ,

2 responses to “Change Shared or Views folder location in an MVC 3 site

  1. Rufus

    13, Nov 2012 at 1:34 am

    Hi, just wanted to mention, I enjoyed this post. It was helpful.
    Keep on posting!

     
  2. anteny tv

    5, Apr 2013 at 4:20 am

    I will right away grasp your rss feed as I can not in finding your email subscription hyperlink or e-newsletter service.
    Do you have any? Kindly permit me realize in order that I could subscribe.
    Thanks.

     

add your comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: