Tuesday, October 15, 2013

Custom View Engine in ASP.NET MVC 4

Custom ViewEngine? Do we really need this? Generally not. But I needed this in a project. In the project I needed to override the ViewEngine.

The override ViewEnging looks like follows:

public class MyViewEngine : RazorViewEngine
    {
        public MyViewEngine()
        {
            ViewLocationFormats = new string[]{
                "/Views/Themes/{0}.cshtml",
                "/Views/{1}/{0}.cshtml"
            };
        }
    }

And then in the Application_Start of Global.asak, add the following

protected void Application_Start()
        {
            // ... other code

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MyViewEngine());

            // ... more code
        }

No comments: