asp.net mvc3 使用母版页
在我的网站中,当用户登录时,我想显示用户名并显示注销按钮。
在 ASP.NET 4.0 中,我们可以使用母版页的代码隐藏文件来为这样的常见事情编写代码。但我不知道如何在MVC3中实现这一点。我不想在每个页面视图上传递用户名并在每个控制器上添加注销操作链接。
有人能建议更好的方法吗?
谢谢萨萨克
In my website, when the user logs in, I want to show the user name and also show a logout button.
In ASP.NET 4.0, we could use the code behind file of the master page to write code for a common thing like this. But I don't know how to achieve this in MVC3. I would not like to pass user name on every page view and add action link of logout on every controller.
Can anyone suggest a better way?
Thanks
Saarthak
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用部分。默认模板正是这样做的。使用内置向导创建一个新的 ASP.NET MVC 3 应用程序,并查看为您生成并在
_Layout.cshtml
_LogOnPartial.cshtml 部分> 使用@Html.Partial("_LogOnPartial")
。该部分如下所示:
它检查用户是否已通过身份验证,如果是,则欢迎他并提供注销链接,如果不是,则仅提供登录链接。
如果您使用 WebForms 视图引擎,则相同的内容:
LogOnUserControl.ascx
,它是使用<% Html.RenderPartial("LogOnUserControl") 从
。Site.Master
调用的; %>You could use a partial. The default template does exactly that. Create a new ASP.NET MVC 3 application using the built-in wizard and look at the
_LogOnPartial.cshtml
partial that's been generated for you and which is invoked in the_Layout.cshtml
using@Html.Partial("_LogOnPartial")
.This partial looks like this:
It checks if the user is authenticated and if it is it Welcomes him and provides a LogOff link and if he isn't it simply provides a LogOn link.
Same stuff if you are using the WebForms view engine:
LogOnUserControl.ascx
which is invoked fromSite.Master
using<% Html.RenderPartial("LogOnUserControl"); %>
.