Pages

Thursday, September 1, 2016

Razor Views Predefined Path View

Setting predefined paths where the Razor View Engine locates partial views.

A Look at the Razor View Engine in ASP.NET MVC

Create a class:

public class PredefinedPathViewEngine : RazorViewEngine
{
    public PredefinedPathViewEngine()
    {
        var locationFormat = new[]
                                    {
                                        "~/Views/{1}/{0}.cshtml",
                                        "~/Views/{1}/Partials/{0}.cshtml",
                                        "~/Views/Shared/{0}.cshtml",
                                        "~/Views/Shared/Partials/{0}.cshtml",
                                    };

        PartialViewLocationFormats = locationFormat.ToArray();
    }

}



Register the class within Global.asax.cs Application_Start() method:


protected void Application_Start()
{
    ViewEngines.Engines.Add(new PredefinedPathViewEngine());

}




This post is for the purpose of my notes only and sometimes a rant.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

No comments:

Post a Comment