System.Web.UI.Page不会让我访问的currentUser或User.Identity在ASP.Net MVC的一个控制器 [英] System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

查看:188
本文介绍了System.Web.UI.Page不会让我访问的currentUser或User.Identity在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?

我检查,我没有叫页另一个类,它为什么会说没有定义的方法?

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 物业工作时,有一个 HTTP处理程序的处理当前请求。作为一个MVC控制器,该请求并没有流传到类,它不会工作。

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不会让我访问的currentUser或User.Identity在ASP.Net MVC的一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆