Razor视图 - 第

Razor视图 - 第

问题描述:

故障我只是得到了一些错误,我不希望看到你...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 

问题是:我该如何使它工作? 任何建议是有价值的!

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

我不知道对付它的最好办法是什么一些可能的解决方法是:

  • 移动RenderSection("left", false)if块体。
  • 处理您的安全控制器,并显示了不同的看法完全如果用户不应该看到的东西(这可能是最好
+0

这很有趣,但我没有使用MVC。 (我只是玩WebMatrix,并试图实现我的想法)这就是为什么处理每个页面上的安全性是一种愚蠢的......但我会尝试重新定向到错误页面。也许它会工作。谢谢你在这里指出我;) – RAMe0 2011-01-25 02:27:10

不幸的是,这个“问题”仍然坚持剃刀(网页实际上是)。我已经看到的一个常见的解决方案(并在需要时实现)是丢弃该部分内容为空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(),但内容在内存暂时缓冲)

这是一个黑客,但它的作品的影响最小;我试图隐藏它作为渲染过程的一部分。