2

I am working with MVC4 and want to set come custom locations for the view engine to look for views in. At present it will only look in the Shared folder outside of the folder it expects the view to exist in.

I want to add 2 more folder locations for it to look for a view in. How can this be achieved in mvc4? I don't want to manipulate how it works at present, just add extra folders for it to look in.

Note: I already work with display modes with views rendered based on the requesting device - I don't want to affect this with my changes.

madth3
  • 7,275
  • 12
  • 50
  • 74
amateur
  • 43,371
  • 65
  • 192
  • 320
  • It looks for a folder that is named the same as the controller and looks in the Shared folder as a last resort. When returning a view you can explicitly tell it where the view lives. If this isn't enough functionality I think you might be organizing your views incorrectly. – The Muffin Man Jan 25 '13 at 17:36

1 Answers1

2

use this code

  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;
        }
    }
  • Will this work ok when using DisplayModes eg. http://www.dotnetexpertguide.com/2011/09/display-mode-in-mvc-4aspnet-mvc-4.html . Thanks. – amateur Jan 25 '13 at 18:03
  • Certainly will - just need clarification on my question? – amateur Jan 25 '13 at 18:05
  • http://stackoverflow.com/questions/909794/how-to-change-default-view-location-scheme-in-asp-net-mvc –  Jan 25 '13 at 18:07