2

我只是遇到了一些我不希望看到的错误...

为了清楚起见,我将显示工作代码和错误代码:

这是有效的

_MainLayout.cshtml

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

页面.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>
}

页面.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

问题是:我怎样才能让它工作?任何建议都很有价值!

4

2 回答 2

2

问题是您仍在子视图中声明该部分,并且剃刀渲染引擎不知道如何处理它。

我不确定处理它的最佳方法是什么可能的解决方法是:

  • 移动块RenderSection("left", false)体的外部。if
  • 在控制器中处理您的安全性,如果用户不应该看到任何东西,则完全显示不同的视图(这可能是可取的
于 2011-01-25T02:04:19.263 回答
1

不幸的是,这个“问题”仍然存在于 Razor (实际上是 WebPages)。我见过的一个常见解决方案(并在需要时实施)是将部分内容静默丢弃为 null 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(),但内容暂时缓冲在内存中

这是一个 hack,但它有效;作为渲染过程的一部分,我试图将其隐藏起来。

于 2013-03-03T22:21:51.840 回答