MVC3- ASP.NET-Logged In Person-different Methods

时间:2011-05-20 10:26:10

标签: asp.net-mvc

我想找出登录的人

以下内容有何区别?

string loggedInName = HttpContext.User.Identity.Name.ToString();

   string loggedInName = HttpContext.Current.User.Identity.Name.ToString();

我问这个问题因为我不能在Controller中使用第二个问题。它给出了以下错误

System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)

2 个答案:

答案 0 :(得分:2)

只有当你不使用线程时,它们才是相同的; System.Web.HttpContext.Current包含与线程关联的值。也就是说,在任何其他线程中,您都无法访问HttpContext.Current。

答案 1 :(得分:1)

  

HttpContext.Current 是一种静态方法   返回当前的http上下文,   而上下文是一种实例方法   在返回的页面类中定义   当前的http上下文(它真的   只需调用上面的函数。)