在 MVC3 中覆盖 RenderBody 内容而不重定向用户
我试图这样做:
<body>
<div id="Page">
<div id="TopBar">
<div id="TopBarLogo">
<img src="@Url.Content("~/Content/images/Weblogo.png")" />
</div>
<div>
@{ Html.RenderAction("MenuPartial", "MenuPartial"); }
</div>
</div>
<div class="Content">
@if (SessionWrapper.IsAuthenticated)
{
@RenderBody()
}
else
{
Html.RenderPartial("AccessDeniedPartial");
}
</div>
</div>
</body>
</html>
但是 ofc 这是不允许的,因为我需要 @RenderBody() 否则会发生异常。 问题是这个解决方案会非常简洁,我仍然想渲染页面的顶部,但我想覆盖内容,除非用户登录。
我能想到的唯一其他解决方案是执行签入控制器并返回另一个视图。问题是我必须在每个控制器中重复这段代码,这让我很难过。
有没有办法全局覆盖与上面类似的内容,而不将用户重定向到另一个网址?如果有的话,请您给我一个正确的方向。我似乎有点陷入目前的心态。
我没有使用默认的 Microsoft 安全类 (FormsAuthentication),在这种情况下我也无法使用。
谢谢。
I was trying to do this:
<body>
<div id="Page">
<div id="TopBar">
<div id="TopBarLogo">
<img src="@Url.Content("~/Content/images/Weblogo.png")" />
</div>
<div>
@{ Html.RenderAction("MenuPartial", "MenuPartial"); }
</div>
</div>
<div class="Content">
@if (SessionWrapper.IsAuthenticated)
{
@RenderBody()
}
else
{
Html.RenderPartial("AccessDeniedPartial");
}
</div>
</div>
</body>
</html>
But ofc this is not allowed, as I need to have @RenderBody() or an exception occurs.
The problem is that this solution would have been so neat, I still want to render the top part of the page, but I would like to override the content unless user is logged in.
The only other solution I can think of is to do the check in the controller and return another view. The problem with that is that I would have to repeat this code in every controller which makes me sad.
Is there any way to globally override the content similar to above, without redirecting the user to another url? If there is, could you please give me a nudge in the right direction. I seem to be a bit stuck in my current mindset.
Im not using the default Microsoft security classes (FormsAuthentication), nor am I able to in this case.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Views/Shared 文件夹中创建一个与
AccessDeniedPartial
内容相同的AccessDenied
视图。保持
RenderBody
不变:创建一个
LogOnAuthorizeAttribute
来检查身份验证,否则显示 AccessDenied 视图。如果您希望将
LogOnAuthorizeAttribute
添加到每个控制器,请将其作为过滤器添加到Global.asax
中的GlobalFilterCollection
中。Create an
AccessDenied
view in the Views/Shared folder that has the same content as yourAccessDeniedPartial
.Leave the
RenderBody
as it is:Create an
LogOnAuthorizeAttribute
that will check for authentication, otherwise show the AccessDenied view.If you want the
LogOnAuthorizeAttribute
to be added to each controller, add it as a filter to theGlobalFilterCollection
inGlobal.asax
.是的,这样做
好吧,不,如果您使用新的 mvc 面向方面编程模型,它基于过滤器属性。检查AuthorizeAttribute和asp.net mvc的过滤器模型。
您可以非常轻松地自定义此 Authorize 属性的行为。
您还可以在这里阅读有关面向方面编程的更多信息:
http://en.wikipedia.org/wiki/Aspect-oriented_programming
yes do that
well no, if you use the new aspect-oriented programming model of mvc, which is based on filter attributes. Check the AuthorizeAttribute and the filter model of asp.net mvc.
You can customize the behavior of this Authorize attribute quite easily.
Also you can read more about aspect oriented programming here:
http://en.wikipedia.org/wiki/Aspect-oriented_programming