我正在开发一个无 cookie 的 ASP.NET WebForms 应用程序,该应用程序需要接收 POST 请求并将请求正文存储到 Session 变量中。该请求被发布到一个 ASPX 页面,我可以用 Postman 模拟。出于某种原因,我无法在页面后面的代码中处理这个问题。
为了检索请求正文,我在 HttpModule 的 Application_EndRequest 处理程序中放置了一些代码,它可以正常工作:
public sealed class MyModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.EndRequest += new EventHandler(Application_EndRequest);
}
private void Application_EndRequest(Object source, EventArgs e)
{
var req = HttpContext.Current.Request;
if (req.UrlReferrer == null &&
req.Url.ToString() == MyEndPoint &&
req.RequestType.ToUpper() == "POST")
{
using (var reader = new StreamReader(req.InputStream, Encoding.UTF8, true, 1024, true))
{
//here I get the body of the POST request
var requestBody = reader.ReadToEnd();
//but HttpContext.Current.Session is null
}
}
}
public void Dispose() {}
}
但是,使用HttpContext.Current.Session. 有什么方法可以访问它吗?
我发现有些人使用该PostRequestHandlerExecute事件。但是,此处理程序似乎没有针对 POST 请求,而是针对 POST 请求正文不再可用的后续 GET 请求。