Find .cshtml in multiple places in MVC 3?

When a user comes to my site, the query string may be template=foo. This value is checked and stored in Session.

My file layout is as follows:

- Views/
  - Templates/
    - test1/
      - Home
        - Index.cshtml
    - test2/
      - Home
        - List.cshtml
  - Home/
    - Index.cshtml

Basically, if a user requests Indexwith template=test1, I want to use Views/Templates/test1/Index.cshtml. If they have template=test2, I want to use Views/Home/Index.cshtml(because it /Views/Templates/test2/Home/Index.cshtmldoes not exist). And if they do not pass the pattern, then it should go directly to Views/Home.

I'm new to MVC and .NET in general, so I'm not sure where to start looking. I use MVC3 and Razor for the viewing engine.

+5
source share
2 answers

, RazorViewEngine ViewLocationFormats. , , WebFormViewEngine, RazorViewEngine :

public class CustomViewEngine : WebFormViewEngine
{
    public CustomViewEngine()
    {
        var viewLocations =  new[] {  
            "~/Views/{1}/{0}.aspx",  
            "~/Views/{1}/{0}.ascx",  
            "~/Views/Shared/{0}.aspx",  
            "~/Views/Shared/{0}.ascx",  
            "~/AnotherPath/Views/{0}.ascx"
            // etc
        };

        this.PartialViewLocationFormats = viewLocations;
        this.ViewLocationFormats = viewLocations;
    }
}
+1
+1

All Articles