Thursday, July 25, 2013

Customize MVC Folder Structure


MVC Architecture comes with its own predefined folder structure and its engine operates only when class is added in respective folder such as controller, model or View. Sometimes it becomes unmanageable  when we’ve large set of source files and views that are required to be maintained. We’ve a workaround by which we can defined our own folder structure. Here I demonstrated partial view scenario where we can have our designed folder for partial view .chtml in place of Shared folder view. To achieve this, just extend [RazorviewEngine] class and add this class file in{ Model/ACARazorViewEngine }and instantiate it in Global.asax- application_start event.

 

Step 1- Add ACARazorViewEngine in Model

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace SampleWeb

{

  

    public class ExtentRazorViewEngine : RazorViewEngine

    {

        public ExtentRazorViewEngine ()   

        {       

            var newLocationFormat = new[]                                   

            {      "~/Views/PartialViews/StaticUCS/{0}.cshtml",

"~/Views/PartialViews/Dialoq/{0}.cshtml"          

            };       

            PartialViewLocationFormats = PartialViewLocationFormats.Union(newLocationFormat).ToArray();   

        }

   

    }

 

}

Step 2: Instantiate the View Engine

  public class MvcApplication : System.Web.HttpApplication

    {

        protected void Application_Start()

        {

            ViewEngines.Engines.Add(new ACARazorViewEngine());

        }

    }

 

No comments :