【问题标题】:Razor View - Troubles with sectionRazor View - 部分问题
【发布时间】:2011-01-25 01:46:40
【问题描述】:

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

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

这是有效的

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

在这种情况下,一切正常,但是当我在 _MainLayout.cshtml 中删除 @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>

在这种情况下,如果用户未通过身份验证,我有这个异常:

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

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

【问题讨论】:

    标签: asp.net razor webmatrix


    【解决方案1】:

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

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

    • RenderSection("left", false) 移到if 块的主体之外。
    • 在控制器中处理您的安全性,并在用户看不到任何内容时完全显示不同的视图(这可能更可取

    【讨论】:

    • 这很有趣,但我没有使用 MVC。 (我只是在玩 WebMatrix 并试图实现我的一些想法)这就是为什么在每个页面上处理安全性有点愚蠢......但我会尝试重定向到错误页面。也许它会起作用。谢谢你指点我;)
    • 啊,当然,我应该学习 WebSecurity 课程。我对 WebMatrix 没有太多经验,但我认为 Razor 的行为方式类似。您可能正在尝试做一些超出 WebMatrix 的事情?
    • 是的,我愿意。我发现 WebMatrix 不像我预期的那样适合我。但它仍然有一些非常有用的东西。现在我使用Context.RedirectLocal 将用户重定向到错误页面,当他没有登录时。这对我来说很好。
    • 我看不出这怎么可能超出 WebMatrix。尽管它面向初学者,或者至少作为一个简单的产品销售,但它背后拥有 .NET Framework 的全部功能。
    【解决方案2】:

    不幸的是,这个“问题”仍然存在于 Razor(实际上是网页)。我见过的一个常见解决方案(并在需要时实施)是将部分内容静默丢弃为空 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,但它有效;作为渲染过程的一部分,我试图将其隐藏起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2013-11-26
      相关资源
      最近更新 更多