System.Web.UI.Page 不允许我从 ASP.Net-MVC 中的控制器访问 CurrentUser 或 User.Identity [英] System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC
问题描述
当我尝试
user = System.Web.UI.Page.CurrentUser
或
user = System.Web.UI.Page.User.Identity
我收到一条错误消息,指出该方法未为 System.Web.UI.Page 定义
I get an error saying that the method is not defined for System.Web.UI.Page
我试图在控制器中访问它,这有关系吗?
I am trying to access it within a Controller, does that matter?
我检查了我没有另一个名为Page的类,为什么会说该方法未定义?
I checked that I do not have another class named Page, Why would it say the method is not defined?
推荐答案
方法很多(基本上都是一样的)
There are many ways to do it (basically, they are all the same)
User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere
Page.User
属性在存在处理当前请求的 Page
HTTP 处理程序时起作用.就像在 MVC 控制器中一样,请求没有被传递给 Page
类,它不会工作.
Page.User
property works when there's a Page
HTTP handler that's processing the current request. As in an MVC controller, the request has not been handed to a Page
class, it won't work.
在控制器中.
这篇关于System.Web.UI.Page 不允许我从 ASP.Net-MVC 中的控制器访问 CurrentUser 或 User.Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!