davy
davy

Reputation: 4562

Can we recursively search sub folders when looking for a view in Asp.Net MVC 4

I know that I can can add search locations to a View Engine as explained in this answer.

I was just wondering is there any way to tell the view engine to then recursively search sub folders without having to specify the entire path?

E.g. if I had a folder structure like /Shared/Partials/Subfolder/Subfolders/MyView

Could I add a search locations like /Shared/Partials/* or similar?

I can't find anything so I don't think it is possible but thought I may as well ask here.

Thanks

Upvotes: 0

Views: 185

Answers (1)

e4rthdog
e4rthdog

Reputation: 5233

What if you use something like this in your viewengine array?

Directory.GetDirectories("c:/somepath/Shared/Partials");

The above returns an array of strings.

So you could write the following:

public class CustomViewEngine : WebFormViewEngine
{
    public CustomViewEngine()
    {
        var viewLocations =  Directory.GetDirectories("c:/somepath/Shared/Partials");

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

And also register your new engine:

protected void Application_Start()
{
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
}

Upvotes: 1

Related Questions