【问题标题】:Getting the `HttpContext` from a generic handler with an `HttpModule`从具有 `HttpModule` 的通用处理程序中获取 `HttpContext`
【发布时间】:2019-05-14 19:35:00
【问题描述】:

我正在开发一个HttpModule 类,用于收集ASP.Net 应用程序的性能和运行时间数据。

我已经弄清楚如何访问Session 以获取有关ASPX 文件和实现IHttpHandler 的类的信息(例如自定义Web 资源或axd 文件),但我找不到方法访问在通用处理程序(ASHX 文件)上传递到 ProcessRequest()HttpContext 实例。

Application.Session 抛出 HttpException

(会话状态在此上下文中不可用)

还有HttpContext.Current.Session == null

谢谢!

【问题讨论】:

    标签: c# asp.net httpmodule asp.net-4.5


    【解决方案1】:

    如果你在你的项目中添加一个新的通用处理程序,它应该看起来像这样

    public class Handler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }
    

    context 作为您可以使用的 HttpContext 传递。

    如果你需要阅读一个 Session 你需要添加IReadOnlySessionStateIRequiresSessionState(区别see here

    using System.Web.SessionState;
    
    public class Handler1 : IHttpHandler, IReadOnlySessionState
    

    【讨论】:

    • 这并不能直接回答我的问题,但它很有用,因为它挑战了我关于传入 HttpContext 的假设。因为我的 HttpModule 必须与我们没有的 ASP.Net 应用程序一起使用'不写,因此无法控制通用处理程序是否可以访问会话状态,我需要考虑这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    相关资源
    最近更新 更多