Change the Views location

女生的网名这么多〃 提交于 2019-11-29 08:11:41

You'll need to create a custom view engine and use that instead. Fortunately you can just inherit from the default one and change the locations on the constructor. Here's a guide to creating your own view engine: http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx

From the article:

protected void Application_Start()
{
    //... other things up here.

    // I want to REMOVE the ASP.NET ViewEngine...
    ViewEngines.Engines.Clear();

    // and then add my own :)
    ViewEngines.Engines.Add(new HoTMeaTViewEngine());
}

public class HoTMeaTViewEngine : VirtualPathProviderViewEngine
{
    public HoTMeaTViewEngine()
    {
        // This is where we tell MVC where to look for our files. This says
        // to look for a file at "Views/Controller/Action.html"
        base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.html" };

        base.PartialViewLocationFormats = base.ViewLocationFormats;
    }
}
griegs

Check this place out. How to change default view location scheme in ASP.NET MVC?

                           base.ViewLocationFormats = new string[] { 
                "~/Views/{1}/{2}/{0}.aspx", 
                "~/Views/{1}/{2}/{0}.ascx", 
                "~/Views/Shared/{2}/{0}.aspx", 
                "~/Views/Shared/{2}/{0}.ascx" ,
                 "~/Views/{1}/{0}.aspx", 
                "~/Views/{1}/{0}.ascx", 
                "~/Views/Shared/{0}.aspx", 
                "~/Views/Shared/{0}.ascx" 

Even easier is this one Can I specify a custom location to “search for views” in ASP.NET MVC?

As an alternative, you can override the view engine locations for a specific controller without affecting the view engines for the other controllers.

These are some snippets from a product I am developing, but it shows the constructor for one of my controllers, and a view engine I made specificially for controllers that inherit from KBRenderMvcController.

So any controller based off KBRenderMvcController will also have my view engine.

However at no point did I clear the view engine collection, which is relevant. Because I wanted the views my product is using to fall back to default locations.

In short, if you delete \App_plugins\Product\Views\MyView And instead create a \Views\MyView it will still render from \Views\MyView instead.

Also in the ViewEngine I demonstrate code that determines the type of controller being used and if it's not a target controller I return empty view locations so they don't get used for other controllers.

    #region Constructor
    public KBRenderMvcController()
        : base()
    {
        viewEngine = new KBFrontEndViewEngine();
        if (!this.ViewEngineCollection.Contains(viewEngine))
            this.ViewEngineCollection.Insert(0, viewEngine);
    }
    #endregion

public class KBFrontEndViewEngine : RazorViewEngine
{
    #region Fields
    private static bool _Initialized = false;
    private static string[] viewLocationFormats = null;
    private static string[] partialViewLocationFormats = null;
    private static string[] viewEngineFileExtensions = new string[] { "cshtml" };
    #endregion

    #region Constructor
    public KBFrontEndViewEngine()
    {            
        if (!_Initialized)
        {
            viewLocationFormats = new string[] 
                    { 
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/{0}.cshtml"), 
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Partials/{0}.cshtml") 
                    };
            partialViewLocationFormats = new string[] 
                    { 
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/Partials/_partial{0}.cshtml"), 
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Partials/_partial{0}.cshtml"),
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/Dialogs/_dialog{1}.cshtml"),
                        string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Dialogs/_dialog{1}.cshtml"),
                    };
            _Initialized = true;
        }
        base.ViewLocationFormats = viewLocationFormats;
        base.PartialViewLocationFormats = partialViewLocationFormats;
        base.MasterLocationFormats = viewLocationFormats;
        base.FileExtensions = viewEngineFileExtensions;
    }
    #endregion

    #region Methods
    //Don't run on requests that are not for our hijacked controllers
    public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
    {
        Type controllerType = controllerContext.Controller.GetType();
        Type baseType = controllerType.BaseType;
        if ((baseType != null) && (baseType.Name == "KBRenderMvcController`1") || (baseType.Name == "KBFrontEndBaseSurfaceController"))
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        else
            return new ViewEngineResult(new List<string>());
    }
    #endregion
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!