asp.net-mvc 在控制器的构造函数中访问 HttpContext.Request
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3432685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing HttpContext.Request in a controller's constructor
提问by ozsenegal
I'm following this ASP.NET MVC tutorial from Microsoft:
我正在关注Microsoft 的这个ASP.NET MVC 教程:
My code is slightly different, where I'm trying to access HttpContext.Request.IsAuthenticated
in the controller's constructor.
我的代码略有不同,我试图HttpContext.Request.IsAuthenticated
在控制器的构造函数中访问。
namespace SCE.Controllers.Application
{
public abstract class ApplicationController : Controller
{
public ApplicationController()
{
bool usuario = HttpContext.Request.IsAuthenticated;
}
}
}
The problem is that HttpContext
is always null.
问题是它HttpContext
始终为空。
Is there a solution to this?
这个问题有方法解决吗?
采纳答案by spender
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
?
Controller 在调用 Index action 之前显着实例化,并且在构造的那一刻 HttpContext 确实不可用。在您的控制器方法中引用它有什么问题Index
?
回答by rob waminal
instead of putting your HttpContext.Request.IsAuthenticated
in Controller level you should put it in Controller Base class that will be inherited in all of your controller with an override method of OnActionExecuting() method.
而不是把你HttpContext.Request.IsAuthenticated
的控制器级别你应该把它放在控制器基类中,该类将通过 OnActionExecuting() 方法的覆盖方法在所有控制器中继承。
In your Controller base you should have
在您的控制器基础中,您应该拥有
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext ctx) {
base.OnActionExecuting(ctx);
ViewData["IsAuthenticated"] = HttpContext.Request.IsAuthenticated;
}
}
and all your Controller should inherit the BaseController class
并且您所有的 Controller 都应该继承 BaseController 类
public class ApplicationController : BaseController
now you should get the ViewData["IsAuthenticated"]
in your Master page.
现在你应该ViewData["IsAuthenticated"]
在你的母版页中得到 。
Edit
编辑
With the link you have given, and relating to what you have done, your ApplicationController
is a Page Controller, not a Base Controller. In the example, ApplicationController
is a Base Controller that is inherited by the HomeController
but what you have done is you are placing the Action method inside your base controller which is the ApplicationController
so your Action Index method will not be invoked when you call any page (Index page) that is not from the ApplicationController.
通过您提供的链接,以及与您所做的相关的工作,您ApplicationController
是页面控制器,而不是基本控制器。在示例中,ApplicationController
是一个由 继承的基本控制器,HomeController
但您所做的是将 Action 方法放置在您的基本控制器中,ApplicationController
这样当您调用任何页面(索引页面)时,您的 Action Index 方法将不会被调用这不是来自 ApplicationController。
回答by Ghini Antonio
I would suggest you use:
我建议你使用:
System.Web.HttpContext.Current.Request
Just remember System.Web.HttpContext.Current
is threadstatic, but if you don't use additional thread the solution works.
请记住System.Web.HttpContext.Current
是线程静态的,但如果您不使用其他线程,则该解决方案有效。
回答by Ricardo Rodriguez
The solution of this problem is to create an override method of Initialize by passing RequestContext object.
这个问题的解决方法是通过传递RequestContext对象来创建Initialize的override方法。
public class ChartsController : Controller
{
bool isAuthed = false;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if (requestContext.HttpContext.User.Identity.IsAuthenticated)
{
isAuthed =true;
}
}
}