2011-01-25 70 views
2

故障我只是得到了一些錯誤,我不希望看到你...Razor視圖 - 第

要清楚,我會告訴工作代碼和代碼錯誤:

這是工作

_MainLayout.cshtml

<div id="content"> 
    <h1>@Page.Title</h1> 
    @RenderSection("left", false) 
    @RenderBody() 
</div> 

Page.cshtml

@section left{ 
<p>Some text on left side</p> 
} 

<div> 
    Some content 
</div> 

在這種情況下,一切工作正常,但當我刪除@RenderSection("left", false)裏面_MainLayout.cshtml我得到異常!我需要哪種情況?見下面的例子:

這不是工作

_MainLayout.cshtml

@if (WebSecurity.IsAuthenticated) { 
    <h1>@Page.Title</h1> 
    @RenderSection("left", false) 
    @RenderBody() 
} else { 
    <h1>You not logged in!</h1> 
    <p>To see this page, you have to login first.</p> 
} 

Page.cshtml

@section left{ 
<p>Some text on left side</p> 
} 

<div> 
    Some content 
</div> 

在這種情況下,如果用戶沒有通過驗證,我有這樣的例外:

描述: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.

異常詳細信息:System.Web.HttpException: Следующие разделы были определены, но не были обработаны для страницы макета "~/_MainLayout.cshtml": "left". 可以翻譯爲:Section was created but wasn't rendered for layout page "~/_MainLayout.cshtml": "left".

源錯誤:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

堆棧跟蹤:

[HttpException (0x80004005): Следующие разделы были определены, но не были обработаны для страницы макета "~/_MainLayout.cshtml": "left".] 
    System.Web.WebPages.WebPageBase.VerifyRenderedBodyOrSections() +91298 
    System.Web.WebPages.WebPageBase.PopContext() +332 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95 
    System.Web.WebPages.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) +102 
    System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +12 
    System.Web.WebPages.WebPageBase.Write(HelperResult result) +67 
    System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +66 
    System.Web.WebPages.WebPageBase.PopContext() +262 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95 
    System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249 

問題是:我該如何使它工作? 任何建議是有價值的!

回答

2

問題是,您仍然在您的子視圖中聲明該部分,並且剃鬚刀渲染引擎不知道如何處理它。

我不知道對付它的最好辦法是什麼一些可能的解決方法是:

  • 移動RenderSection("left", false)if塊體。
  • 處理您的安全控制器,並顯示了不同的看法完全如果用戶不應該看到的東西(這可能是最好
+0

這很有趣,但我沒有使用MVC。 (我只是玩WebMatrix,並試圖實現我的想法)這就是爲什麼處理每個頁面上的安全性是一種愚蠢的......但我會嘗試重新定向到錯誤頁面。也許它會工作。謝謝你在這裏指出我;) – RAMe0 2011-01-25 02:27:10

1

不幸的是,這個「問題」仍然堅持剃刀(網頁實際上是)。我已經看到的一個常見的解決方案(並在需要時實現)是丟棄該部分內容爲空TextWriter

@if (WebSecurity.IsAuthenticated) { 
    <h1>@Page.Title</h1> 
    @RenderSection("left", false) 
    @RenderBody() 
} else { 
    <h1>You not logged in!</h1> 
    <p>To see this page, you have to login first.</p> 
    @{ 
     WriteTo(TextWriter.Null, RenderSection("left")); 
    } 
} 

呼叫到RenderSection滿足調用對應RenderSection對於每一個部分的施加規定定義。傾銷TextWriter.Null丟棄的內容,並確保內存消耗(其他實現方式已經使用new StringWriter(),但內容在內存暫時緩衝)

這是一個黑客,但它的作品的影響最小;我試圖隱藏它作爲渲染過程的一部分。