.net – How to change default view location scheme in ASP.NET MVC


I want to change view locations at runtime based on current UI culture. How can I achieve this with default Web Form view engine?

Basically I want to know how implement with WebFormViewEngine something what is custom IDescriptorFilter in Spark.

Is there other view engine which gives me runtime control over view locations?

Edit: My URLs should looks following {lang}/{controller}/{action}/{id}. I don't need language dependent controllers and views are localized with resources. However few of the views will be different in some languages. So I need to tell view engine to looks to the language specific folder first.

Best Solution

A simple solution would be to, in your Appication_Start get hold of the appropriate ViewEngine from the ViewEngines.Engines collection and update its ViewLocationFormats array and PartialViewLocationFormats. No hackery: it's read/write by default.

protected void Application_Start()
    // Allow looking up views in ~/Features/ directory
    var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
    razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(new string[] 
    // also: razorEngine.PartialViewLocationFormats if required

The default one for Razor looks like this:

ViewLocationFormats = new string[]

Note that you may want to update PartialViewLocationFormats also.