2011-01-25 2 views
2

とのトラブル...レイザービュー - 私はちょうど私が見に期待していないいくつかのエラーを得た部分

明確にするために、私はエラーで動作するコードとコードを紹介します:

これは、作業

_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の中に削除した場合、例外が発生します。どのケースが必要ですか?ユーザーが認証されていない場合

@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> 

に、私はこの例外を持って

_MainLayout.cshtmlが動作していない

これ以下の例を参照してください:

説明: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

問題は、子ビューのセクションを宣言していて、カミソリレンダリングエンジンがその処理を知らないということです。

    ifブロックのボディの RenderSection("left", false)外を移動し
  • は、私はそれに対処するための最良の方法は、いくつかの可能な回避策があるがどうなるかわかりません。

  • コントローラにおけるプロセスセキュリティを、ユーザーが何も表示されません場合は完全に異なるビューを表示する(これはおそらく望ましいです)
+0

これは面白いですが、私はMVCを使用していません。 (私はWebMatrixで遊んでいて、私の考えを実現しようとしています)なぜ各ページのセキュリティを処理するのが愚かなのですか...しかし、私はエラーページにリダイレクトしようとします。多分それはうまくいくでしょう。ありがとうございます;) – RAMe0

+0

確かに、私はWebSecurityクラスを選んだはずです。私はWebMatrixで多くの経験を持っていませんが、私はRazorも同様の動作をすると想定しています。あなたはWebMatrixを少し超えて何かをしようとしているかもしれませんか? –

+0

はい、あります。私はWebMatrixが私が期待していたものと一致しないと考えました。しかし、それはまだ非常に有用なものがあります。今私は 'Context.RedirectLocal'を使用しています。これはログインしていないときにユーザーをエラーページにリダイレクトしています。これはうまく動作します。 – RAMe0

1

は残念ながら、この「問題」はまだ(剃刀で解消されません実際にWebページは)。私が見た共通の溶液(を必要なときに実施)サイレントヌル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()を使用しているが、一時的にメモリにバッファリングコンテンツ)

それはハックですが、それは動作します。私はレンダリングプロセスの一部としてそれを隠すようにしています。

関連する問題