2010-07-29 31 views
2

我想知道其他人是否有這個相同的問題,或者它是否只是我!渲染部分與父視圖相同的名稱 - 崩潰WebDev.WebServer40.exe

鑑於我有一個觀點Purchases.aspx和局部視圖Purchases.ascx

Purchases.aspx如果我這樣做:Html.RenderPartial("Purchases")然後WebDev.WebServer40.exe基本關閉。

我猜這是由堆棧溢出引起的,因爲RenderPartial無法確定它應該呈現的內容(.aspx或.ascx)。

這是一個錯誤,它是一種定義的行爲,還是它正在爲我發生?

回答

7

它是定義的行爲,因爲ViewLocationFormats和PartialViewLocationFormats的定義如下,首先會查看一個aspx頁面。

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

PartialViewLocationFormats = ViewLocationFormats; 

PartialViewLocationFormats應該排除我認爲的aspx定義。覆蓋默認的WebFormViewengine可以解決此問題。請注意,您需要在Application_Start()方法中註冊此方法

public class ASPXViewEngine: WebFormViewEngine 
{ 
    public ASPXViewEngine() 
    { 
     base.PartialViewLocationFormats = 
       new string[] 
        { 
         "~/Views/{1}/{0}.ascx", 
         "~/Views/Shared/{0}.ascx" 
        }; 

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

謝謝艾哈邁德,非常有用! – 2010-07-29 16:45:38