我如何从一个HttpContextBase HttpContext对象在ASP.NET MVC 1? [英] How do I get an HttpContext object from HttpContextBase in ASP.NET MVC 1?

查看:328
本文介绍了我如何从一个HttpContextBase HttpContext对象在ASP.NET MVC 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一些的WebForms / MVC无关的工具工作,我需要获得的HttpContext 给出一个参考 HttpContextBase的一个实例对象。我不能使用 HttpContext.Current ,因为我需要这个工作异步以及( HttpContext.Current 收益异步请求时)。我知道 HttpContextWrapper ,但去了错误的方式。

I'm working with some WebForms/MVC-agnostic tools, and I need to get an instance of HttpContext given a reference to an HttpContextBase object. I can't use HttpContext.Current because I need this to work asynchronously as well (HttpContext.Current returns null during an asynchronous request). I'm aware of HttpContextWrapper, but goes the wrong way.

推荐答案

最简单的方法是让该应用程序,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.applicationinstance.aspx\"><$c$c>ApplicationInstance,并使用其<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpapplication.context.aspx\"><$c$c>Context属性:

The simplest way is to get the application, ApplicationInstance, and use its Context property:

// httpContextBase is of type HttpContextBase
HttpContext context = httpContextBase.ApplicationInstance.Context;

<子>(感谢伊斯梅尔Smyrnow 谁在评论中指出这一点)

(thanks to Ishmael Smyrnow who noted this in the comments)

您可以的,特别是如果你已经交到 HttpContextBase 实例键入<一的href=\"http://msdn.microsoft.com/en-us/library/system.web.httpcontextwrapper.aspx\"><$c$c>HttpContextWrapper在运行时。下面的例子说明你如何能做到这一点。它假定你有一个名为方法接受上下文 HttpContextBase 但后来需要调用的方法在三阶党的组件(你可能不会有好运气修改),其预期的背景下键入为的HttpContext

You can, especially if the HttpContextBase instance you've been handed is of type HttpContextWrapper at run-time. The following example illustrates how you can do this. It supposes you have a method called Foo that accepts context as HttpContextBase but then needs to call a method in a third-party assembly (that you may not have the good fortune to modify) that is expecting the context to be typed as HttpContext.

void Foo(HttpContextBase context) 
{
    var app = (HttpApplication) context.GetService(typeof(HttpApplication));
    ThirdParty.Bar.Baz(app.Context);
}

// Somewhere in assembly and namespace ThirdParty,
// in a class called Bar, there is Baz expecting HttpContext:

static void Baz(HttpContext context) { /* ... */ }

HttpContextBase 有一个名为方法<一href=\"http://msdn.microsoft.com/en-us/library/system.web.httpcontextwrapper.getservice.aspx\"><$c$c>GetService作为支持的结果<一个href=\"http://msdn.microsoft.com/en-us/library/system.iserviceprovider.aspx\"><$c$c>IServiceProvider. HttpContextWrapper 委托给的GetService 实施的的GetService 覆盖包装的的HttpContext 实例。在的GetService 实施的HttpContext 允许您查询秋后算账像的HttpApplication 的Htt prequest 的Htt presponse 等。它只是恰巧,的HttpApplication 有一个名为的上下文并返回的HttpContext 的一个实例。因此,人们在包裹的HttpContext 例如,通过询问 HttpContextBase 的HttpApplication 通过的GetService 接着读上下文返回的HttpApplication 实例。

HttpContextBase has a method called GetService as a result of supporting IServiceProvider. The GetService override of HttpContextWrapper delegates to the GetService implementation of the wrapped HttpContext instance. The GetService implementation of HttpContext allows you to query for usual suspects like HttpApplication, HttpRequest, HttpResponse and so on. It just so happens that HttpApplication has a property called Context and which returns an instance of HttpContext. So one gets at the wrapped HttpContext instance by asking HttpContextBase for HttpApplication via GetService followed by reading the Context property of the returned HttpApplication instance.

HttpContextBase 的GetService 不会显示为的HttpContext 但那是因为的HttpContext 工具 IServiceProvider.GetService 明确地,而 HttpContextBase 没有。​​

Unlike HttpContextBase, GetService does not appear as a public member of HttpContext but that is because HttpContext implements IServiceProvider.GetService explicity while HttpContextBase doesn't.

请记住不再是可测试的,因为它依赖于能够在解开底层的HttpContext 测试和这是摆在首位,几乎不可能假冒/存根。这个答案的要点,但是,是要解决问题,我如何从一个HttpContextBase HttpContext对象?的的字面的。图示的方法是在你发现自己夹组件之间的情况下非常有用,你不一定拥有的奢侈品修改。

Bear in mind that Foo is no longer testable because it relies on being able to unwrap the underlying HttpContext during testing and which is next to impossible to fake/stub in the first place. The point of this answer, however, is to address the question, "How do I get an HttpContext object from HttpContextBase?", literally. The illustrated technique is useful in those situations where you find yourself sandwiched between components you don't necessarily have the luxury to modify.

这篇关于我如何从一个HttpContextBase HttpContext对象在ASP.NET MVC 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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