【问题标题】:RenderBody() method in an if statement?if 语句中的 RenderBody() 方法?
【发布时间】:2015-06-08 13:15:06
【问题描述】:

我有一个项目,其中包含许多具有相同布局的视图,因此我使用 _ViewStart 使它们都具有相同的布局。在该布局中,我使用 @RenderBody() 将页面内容填充到布局中。这行得通。

现在我正在尝试将 @RenderBody() 放入 if 语句中。在 else 语句中,我放了一些 html。它看起来像这样:

    ...*/
    @if (condition == true) { 
        @RenderBody()
    } else {
        <p>Some text if the condition is false</p>
    }
    /*...

当我运行这段代码并且条件为假时,我得到这个错误:

System.Web.HttpException:尚未为布局页面“~/Views/Shared/_BasicLayout.cshtml”调用“RenderBody”方法。

所以我的问题是:

如何将 @RenderBody() 放入 if 语句中?

根据要求,这是整个 _BasicLayout 正文:

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li @(@ViewBag.Title == "Films" ? "class=active" : "")>@Html.ActionLink("Films", "List", "FilmSelectie")</li>
                <li @(@ViewBag.Title == "Index" ? "class=active" : "")>@Html.ActionLink("Voorstellingen", "Index", "Voorstelling")</li>
                <li @(@ViewBag.Title == "Nieuwsbrief" ? "class=active" : "")>@Html.ActionLink("Nieuwsbrief", "Nieuwsbrief", "Nieuwsbrief")</li>
                <li @(@ViewBag.Title == "Contact" ? "class=active" : "")>@Html.ActionLink("Contact", "ContactBreda", "Contact")</li>
                <li @(@ViewBag.Title == "Login" ? "class=active" : "")>@Html.ActionLink("Login", "Login", "User")</li>
            </ul>
        </div>
    </div>
</nav>
<div class="row panel" style="display:block; box-shadow: none;margin-top:5px;margin:0;margin-left:0px;">
    <div id="film informatie" class="col-xs-3">
    </div>
    <div class="col-xs-8" style="display:block; box-shadow:none; margin-top:20px;">
        @if (0 == 1) { 
            @RenderBody()
        } else {
            <h2>Geen toegang!</h2>
            <p>U heeft geen toegang tot deze pagina. Log in met een geschikt account om op deze pagina te komen.</p>
        }
    </div>
</div>
<footer>
    <div class="panel-footer" style="display:block; text-align:center;">
        <p>
            © 2015 Cinema Jam || Chasséveld 1, Breda ||
            <a  href="https://www.facebook.com/CinemaJamBreda" target="_blank"><img style="width:20px; height:20px;" src="~/Content/facebook.png" alt="CinemaJam FacebookPagina"></a>
            <a href="https://twitter.com/CinemaJamB3" target="_blank"><img style="width:20px; height:20px;"  src="~/Content/twitter.png" alt="CinemaJam TwitterPagina"></a>
        </p>
    </div>
</footer>

(不知何故,我无法将标题和正文的开头粘贴到堆栈溢出中,它包含对引导程序的引用)

【问题讨论】:

  • 您要检查什么条件,为什么?似乎您正在尝试将逻辑放入您的布局中,而不是属于您的视图。
  • 你能告诉我们你的_BasicLayout.cshtml是什么样的吗?创建了一个测试项目,我可以简单地从_Layout.cshtml 中删除@RenderBody() 而不会出现问题。
  • 我正在尝试在每个页面上添加一个检查,以检查用户是否可以访问它。
  • @Janneman96 你使用的是什么版本的 MVC?你有充分的理由不使用[Authorize]吗?
  • 这是XY Problem 的示例。当您应该首先说明该解决方案的原因时,您询问了您提出的解决方案。不,你在做什么不是一个好主意。在 MVC 中处理授权的方法非常完善,其中之一是 IAuthorizationFilter 属性,例如上面提到的 [Authorize] 属性。看看那些!

标签: c# asp.net-mvc razor layout


【解决方案1】:

解决这个问题的方法是使用[Authorize]。我现在正在关注本教程: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF 感谢 DionV。

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2013-11-24
    • 2019-11-15
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2013-10-24
    相关资源
    最近更新 更多