访问HttpContext.Request在控制器的构造函数 [英] accessing HttpContext.Request in a controller's constructor
本文介绍了访问HttpContext.Request在控制器的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面的微软<这个 ASP.NET MVC教程/ A>
I'm following this ASP.NET MVC tutorial from Microsoft:
我的code是略有不同,在那里我试图访问 HttpContext.Request.IsAuthenticated
在控制器的构造函数。
My code is slightly different, where I'm trying to access HttpContext.Request.IsAuthenticated
in the controller's constructor.
namespace SCE.Controllers.Application
{
public abstract class ApplicationController : Controller
{
public ApplicationController()
{
bool usuario = HttpContext.Request.IsAuthenticated;
}
}
}
问题是,的HttpContext
总是空。
有没有解决这个?
推荐答案
控制器被实例化显著之前到被调用Index操作的点,在施工的HttpContext的时刻确实是不可用。这有什么错在你的控制器方法引用它首页
?
The Controller is instantiated significantly prior to the point where the Index action is invoked, and at the moment of construction HttpContext is indeed unavailable. What's wrong with referencing it in your controller method Index
?
这篇关于访问HttpContext.Request在控制器的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文