![](/img/trans.png)
[英]How do I access properties from page in ContentPlaceholder in my MasterPage
[英]How do I show Welcome “CurrentUserName” at the top of my MasterPage in MVC environment?
有人告诉我可以在任何Controller中创建ActionResult并使用User.Identity.Name返回PartialView。 然后,我可以在我的母版页中使用RenderPartialView来显示用户名。 我正在尝试实现它,但是一直出现错误,即系统看不到我的PartialView。 有人可以详细解释如何实现吗? 非常感谢你
不幸的是,该项目的结构方式无法从Master内部呈现Logon。 从WebConfig调用它,并在AccountController中进行身份验证。
<authentication mode="Forms">
<forms loginUrl="~/Account" timeout="2880"/>
</authentication>
在创建新的MVC项目时,VS在弹出的基本模板中已经有一个工作示例,在site.master中,您有一个Html.RenderPartial(“ LogOnUserControl”)来呈现LogOnUserControl局部视图(位于如果用户已通过身份验证,则该控件将显示User.Identity.Name。
除非这不是您的意思,否则在这种情况下,我认为我们需要对您的问题进行更多说明:)
创建新的MVC项目时,如果查看LogOnUserControl.ascx文件,则会注意到当Request.IsAuthenticated为true时,将执行此代码行。
<%= Html.Encode(Page.User.Identity.Name) %>
在Views \\ Shared目录内的Site.Master中,您将看到LogOnUserControl呈现到如下页面:
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.