在哪里使用Controller.HttpContext

发布于 2024-08-26 15:49:07 字数 391 浏览 12 评论 0原文

在我的基本控制器的构造函数中,我调用一个扩展方法来检查客户端上的特定 cookie。

目前我正在使用 System.Web.HttpContext.Current 来获取当前上下文。

但是,我认为我应该使用 Controller.HttpContext,因为它更易于测试并且包含有关请求的其他信息。

但是,Controller.HttpContext 在创建时返回 null(相信这是设计使然),而且在初始化和执行方法上也会返回 null(除非我使用 Routing.RequestContext.HttpContext?)。

因此,如果我应该使用 Controller.HttpContext 而不是 HttpContext.Current,那么我什么时候可以在请求中使用它?

谢谢 本

In my base controller's constructor I am calling an extension method that checks for specific cookies on the client.

Currently I am using System.Web.HttpContext.Current to get the current context.

However, I am lead to believe that I should be using Controller.HttpContext since it is more testable and contains additional information about the request.

However, Controller.HttpContext returns null on creation (believe this is by design) but also on Initialize and Execute methods (unless I use Routing.RequestContext.HttpContext?).

So if I should be using Controller.HttpContext instead of HttpContext.Current, at what point is it available to me in a request?

Thanks
Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-09-02 15:49:07

当您在控制器内调用操作方法时,您可以获取 Controller.HttpContext。 您可以在操作方法中访问它,

因此,这意味着如果您想在每个请求上进行检查,

public class LoggingFilterAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
        filterContext.ActionDescriptor.ActionName);

    base.OnActionExecuting(filterContext);
  }

  public override void OnActionExecuted(ActionExecutedContext filterContext)
  {
    if (filterContext.Exception != null)
        filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");

    base.OnActionExecuted(filterContext);
  }
}

也许您可​​以使用自定义属性,看看这个示例:我建议您阅读自定义属性。但更可测试是什么意思?您可以使用像 rhino mocks 或 google moq 这样的模拟框架轻松模拟您的 httpcontext

You can get your Controller.HttpContext when you invoke an action method inside your controller. So that means that you can access it inside an action method

if you want to check that on every request maybe you can use an custom attribute look at this example:

public class LoggingFilterAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
        filterContext.ActionDescriptor.ActionName);

    base.OnActionExecuting(filterContext);
  }

  public override void OnActionExecuted(ActionExecutedContext filterContext)
  {
    if (filterContext.Exception != null)
        filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");

    base.OnActionExecuted(filterContext);
  }
}

I suggest you read up on custom attributes. But what do you mean with more testable? You can easily mock your httpcontext with a mocking framework like rhino mocks or google moq

无需解释 2024-09-02 15:49:07

如果您关心可测试性,我会用一个接口包装对 HttpContext 的访问,并将其解析/注入到您的控制器中。

public class CookieValidator : ICookieValidator
{
   private HttpContext _Context;
   public HttpContext Context
   {
      get
      {
         if(_Context == null)
         {
             _Context = HttpContext.Current;
         }
         return _Context;
      }
      set  // set a mock here when unit testing
      {
         _Context = value;
      }
   }

公共 bool HasValidCookies() { _Context... // 在这里做你的逻辑 } }

If testability is your concern, I would wrap up the access to HttpContext with an interface and resolve it/inject it into your controller.

public class CookieValidator : ICookieValidator
{
   private HttpContext _Context;
   public HttpContext Context
   {
      get
      {
         if(_Context == null)
         {
             _Context = HttpContext.Current;
         }
         return _Context;
      }
      set  // set a mock here when unit testing
      {
         _Context = value;
      }
   }

public bool HasValidCookies() { _Context... // do your logic here } }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文