更改视图位置

时间:2010-04-29 06:40:30

标签: asp.net asp.net-mvc asp.net-mvc-2

我正在开发一个MVC 2.0网站。我想更改我网站中的View文件夹位置。我想将views文件夹保存在其他文件夹中,当我尝试这样做时,我会收到以下错误

The view 'Index' or its master was not found. The following locations were searched:
~/Views/Search/Index.aspx
~/Views/Search/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

My Views文件夹将位于〜/ XYZ / ABC / Views中,而不是〜/ Views。请解决我的问题。我是否会遇到任何问题如果我更改默认的Views文件夹位置。我是否需要更改HTML Helper类中的任何内容,因为我在MVC中什么都不知道,因为这是我的开始项目,我不想冒险。请帮助我......

3 个答案:

答案 0 :(得分:5)

您需要创建自定义视图引擎并使用它。幸运的是,您可以从默认值继承并更改构造函数上的位置。以下是创建自己的视图引擎的指南:http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx

来自文章:

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;
    }
}

答案 1 :(得分:3)

检查这个地方。 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" 

这个Can I specify a custom location to “search for views” in ASP.NET MVC?

更容易

答案 2 :(得分:0)

作为替代方案,您可以覆盖特定控制器的视图引擎位置,而不会影响其他控制器的视图引擎。

这些是我正在开发的产品的一些片段,但它显示了我的一个控制器的构造函数,以及我专门为从KBRenderMvcController继承的控制器创建的视图引擎。

因此,任何基于KBRenderMvcController的控制器都将拥有我的视图引擎。

但是我没有清除视图引擎集合,这是相关的。因为我希望我的产品使用的视图可以回退到默认位置。

简而言之,如果你删除\ App_plugins \ Product \ Views \ MyView而是创建一个\ Views \ MyView,它仍然会从\ Views \ MyView渲染。

同样在ViewEngine中,我演示了确定所使用控制器类型的代码,如果它不是目标控制器,则返回空视图位置,以便它们不会用于其他控制器。

    #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
}