【问题标题】:IHttpHandler and continue to page with PageParser.GetCompiledPageInstanceIHttpHandler 并继续使用 PageParser.GetCompiledPageInstance 页面
【发布时间】:2026-01-17 00:10:02
【问题描述】:

我正在尝试做一个简单的 HttpHandler 来检查一些事情(安全性 - 虽然这并不重要)并继续访问页面。它应该很简单,但我显然有1个或两个参数GetCompiledPageInstance不正确:

public void ProcessRequest(HttpContext context)
{
    if (CheckAccess(context))
        PageParser.GetCompiledPageInstance(context.Request.Path, context.Request.PhysicalPath, context);
}

public bool IsReusable { get { return false; } }

private bool CheckAccess(HttpContext context)
{
    return true;
}

这是一个网站,而不是应用程序,尽管我认为这没什么区别。

当我将处理程序代码添加到网络配置时

<add name="SecurityHandler" verb="*" path="*.aspx" type="SecurityHandler" />

现在我收到一个在添加之前没有收到的错误(没有其他更改):

只有在 enableSessionState 设置为 true 时才能使用会话状态,无论是在配置文件中还是在 Page 指令中。还请确保 System.Web.SessionStateModule 或自定义会话状态模块包含在应用程序配置的 \\ 部分中。

没关系。找到第二部分的答案:Problem with HttpHandler and session state

在处理程序中实现 IRequiresSessionState

【问题讨论】:

  • 我相信 GetCompiledPageInstance 中的第二个参数需要只是文件名而不是文件名和路径。尝试使用 System.IO.Path.GetFileName(context.Request.PhysicalPath);

标签: c# asp.net httphandler ihttphandler


【解决方案1】:

试试:

    PageParser.GetCompiledPageInstance(context.Request.Path, context.Request.PhysicalPath, context )
         .ProcessRequest( context );

【讨论】: