ASP.NETControllerBaseClassUser.Identity.Name如本文所述,我创建了一个抽象基控制器类,能够从控制器到master.page。在这种情况下,我想在我的数据库中查找用户,查询User.Identity.Name(仅当他已登录时)。但是,我注意到在这个抽象基类中,User属性始终为null。为此,我应该怎么做?非常感谢正如Paco所建议的那样,viewdata在您尝试使用它之前不会被初始化。尝试覆盖Controller.Initialize():publicabstractclassApplicationController:Controller{privateIUserRepository_repUser;publicApplicationController(){}protectedoverridevoidInitialize(System.Web.Routing.RequestContextrequestContext){base.Initialize(requestContext);_repUser=RepositoryFactory.getUserRepository();varloggedInUser=_repUser.FindById(User.Identity.Name);ViewData["LoggedInUser"]=loggedInUser;要使用此用户,您应该从HttpContext.Current.User.Identity获取当前页面。名称通过在web.config中设置对窗口的身份验证,您可以使用User.Identity.Name获取用户我正在使用Page类而不是静态Utlilites类。像那样;页面P=(页面)HttpContext.Current.Handler;我可以通过P对象获取当前请求页面的所有属性。你试过这个吗:ControllerContext.HttpContext.Current.User.Identity.Name?以上就是C#学习教程的全部内容:ASP.NET控制器基类User.Identity.Name。代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
