2010-05-06 60 views
1

我使用了下面的ASP.NET MVC v1和v2以下的代碼,但是當我今天向我的應用程序添加了Area時,該區域的控制器不能「 t在我的Areas/Views/controllerView文件夾中找到任何視圖。它發佈了非常着名的例外情況,它搜索了這4個標準文件夾,但沒有看到區域下方。ASP.NET MVC 2中的自定義視圖引擎不能與區域一起工作

如何更改代碼以便它可以與區域一起使用?也許在ASP.NET MVC 2支持區域自定義視圖引擎的例子?關於它在網絡上的信息是非常scarse ..

下面的代碼:

public class PendingViewEngine : VirtualPathProviderViewEngine 
{ 
    public PendingViewEngine() 
    { 
     // This is where we tell MVC where to look for our files. 
     /* {0} = view name or master page name  
     * {1} = controller name  */ 
     MasterLocationFormats = new[] {"~/Views/Shared/{0}.master", "~/Views/{0}.master"}; 
     ViewLocationFormats = new[] 
           { 
            "~/Views/{1}/{0}.aspx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx", 
            "~/Views/{1}/{0}.ascx" 
           }; 
     PartialViewLocationFormats = new[] {"~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.ascx"}; 
    } 

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) 
    { 
     return new WebFormView(partialPath, ""); 
    } 

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
    { 
     return new WebFormView(viewPath, masterPath); 
    } 
} 

回答

0

當你創建了一個區域,它是否創造AreaRegistration類?如果是這樣,你在global.asax.cs有這個嗎?顧名思義,它用MVC註冊了這些區域。

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
    } 
+0

我確實有這個,我有地區註冊類,但問題依然存在。問題是,我們的視圖引擎會重置文件夾的某些路徑,以允許在各自文件夾中的視圖和共享文件夾之間進行鏈接,如您所見。 – mare 2010-05-06 19:21:14

+0

編寫附加視圖引擎時,這些區域不相關。 http://aspnet.codeplex.com/sourcecontrol/network/Show?projectName=aspnet&changeSetId=23011#266536是默認的WebFormViewEngine,並且它沒有關於區域的代碼。這就是我認爲它可能與配置有關的原因。 – Jab 2010-05-06 19:49:07

+0

感謝您的建議,但它必須是視圖引擎問題,因爲如果我從global.asax.cs註釋掉它,它會發現區域視圖沒有問題。 – mare 2010-05-06 20:08:26

2

...搜索那些4個標準文件夾,但它並沒有下區

這實際上是一個提示一下 - MVC不知道在哪裏以及如何尋找,因爲位置的地區查看尚未在您的自定義視圖引擎中定義。

您可能需要設置AreaPartialViewLocationFormats並在ViewLocationFomats屬性中包含Areas位置,因爲這是一個已啓用區域的應用程序。

ViewLocationFormats = new[] 
{ 
    "~/Areas/Views/{1}/{0}.aspx", 
    ... 
}; 

,並可能......

AreaPartialViewLocationFormats = new[] 
{ 
    "~/Areas/{1}/Views/{0}.ascx", 
    "~/Areas/Views/{1}/{0}.ascx", 
    "~/Views/Shared/{0}.ascx" 
}; 

兩個引用:

  1. MSDN < - 大概是因爲更新MVC1 包括新的領域 的東西,因此爲什麼它不工作
  2. The Haack < - 舊視圖引擎在Global.asax需要修改的文章,但一個很好的介紹和概述
3

不是你的問題的直接回應,但一些其他的讀者可能會發現有用,使用自定義:

void Application_Start(object sender, EventArgs e) 
{ 
    RegisterRoutes(RouteTable.Routes); 
    ViewEngines.Engines.Clear(); 
    ViewEngines.Engines.Add(new PendingViewEngine()); 
} 
  • 馬特