2010-04-29 303 views
1

我正在開發一個MVC 2.0網站。我想更改我的網站中的查看文件夾位置。我想保留意見文件夾中的其他文件夾裏面,當我嘗試這樣做,我收到以下錯誤更改視圖位置

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. 

我的視圖文件夾將在〜/ XYZ/ABC /瀏覽次數,而不是〜/瀏覽次數。請解決我的問題。我會得到任何問題如果我更改默認的視圖文件夾位置。我是否需要更改HTML Helper類任何東西,因爲我不知道在什麼MVC,因爲這是我的出發項目我不想risk..Please幫我...

回答

4

你需要創建一個不是自定義視圖引擎和使用。幸運的是,您可以繼承默認值並更改構造函數中的位置。這裏有一個指導創建自己的視圖引擎: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; 
    } 
} 
+0

如果你不想幹到底,並創建自己的ViewEngine你可以設置的位置格式在App_Start現有的發動機:ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(新WebFormViewEngine { LocationFormats =新的字符串[] { 「〜/查看/ {1}/{0}的.html」} ... }); – Cargowire 2010-12-20 18:38:05

0

作爲替代方案,您可以覆蓋特定的控制器視圖引擎的位置,而不會影響視圖引擎爲其他控制器。

這些是我正在開發的產品的一些片段,但它顯示了我的控制器之一的構造函數,以及我特別爲從KBRenderMvcController繼承的控制器製作的視圖引擎。

因此,任何基於KBRenderMvcController的控制器都將擁有我的視圖引擎。

然而,我沒有清除視圖引擎集合,這是相關的。因爲我希望我的產品正在使用的視圖回退到默認位置。

總之,如果您刪除\ App_plugins \產品\查看\ MyView的,而是創建一個\查看\ MyView的它仍然從\瀏覽渲染\ MyView的來代替。

另外,在視圖引擎我演示代碼,確定所用控制器的類型,如果它不是一個目標控制器我回空視圖位置,這樣它們纔不會被用於其他控制器。

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