gpt4 book ai didi

c# - 使用 HttpModule 而不是 Global.asax

转载 作者:太空狗 更新时间:2023-10-30 01:12:25 40 4
gpt4 key购买 nike

我如何转换:

<%@ Application Language="C#" %>

<script runat="server">

void Session_Start(object sender, EventArgs e)
{


}

</script>

使用 HttpModule 的方案?

此外,我可以将 Global.asax 编写为纯 C# 而不是使用标记吗?

最佳答案

在自定义模块的初始化中,您需要检索 session 模块并为 Start 事件添加事件处理程序。

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Begin_Request);
IHttpModule sessionModule = context.Modules["Session"];
if(sessionModule != null &&
sessionModule.GetType() == typeof(System.Web.SessionState.SessionStateModule))
{
(sessionModule as System.Web.SessionState.SessionStateModule).Start
+= new EventHandler(CustomHttpModule_Start);
}
}

Also, can I write the Global.asax aspure C# instead of using tags?

是的,您可以在 Global.asax 中添加代码并将内容更改为

<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>

后面的代码应该继承自 System.Web.HttpApplication

public class Global : System.Web.HttpApplication
{
public Global() { }

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
}

关于c# - 使用 HttpModule 而不是 Global.asax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/490961/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com