MVC3 group views in subfolders in the main action folder

eg. I have three payment controllers, each of which refers to a third-party payment processor, so in my "Registry" folder I have one folder for each of these controllers. I would like to move them to Views \ Payments \ Processor1, Views \ Payments \ Processor2, etc. Instead of current Views \ Processor1, etc.

I am not ready to implement areas yet, so I hope that somehow I can tell MVC to also look in subfolders or something like that. Can this be done and how?

+5
source share
2 answers

You can write a custom view engine and override the default location :

public class MyRazorViewEngine : RazorViewEngine
{
    public MyRazorViewEngine() : base()
    {
        base.ViewLocationFormats = base.ViewLocationFormats.Concat(new[] {
            "~/Views/Payments/{1}/{0}.cshtml",
            "~/Views/Payments/{1}/{0}.vbhtml"
        }).ToArray();

        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Concat(new[] {
            "~/Views/Payments/{1}/{0}.cshtml",
            "~/Views/Payments/{1}/{0}.vbhtml"
        }).ToArray();
    }
}

and then register it in Application_Start:

ViewEngines.Engines.Add(new MyRazorViewEngine());
+11
source

Do you need searches to be searched? You can specify which view to use in the View () call, complete with the outline.

+2
source

All Articles