c# - 如何在 global.asax.cs 中运行事件
问题描述
我使用 Visual Studio 2019 创建一个新的 webforms 项目。我在 中写了一个Session_Start
事件Global.asax.cs
,但它永远不会触发!
这是一些代码 -Global.asax
<%@ Application CodeBehind="Global.asax.cs" Inherits="webApp.Global" Language="C#" %>
Global.asax.cs
:
namespace webApp
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Application["test"] = "123";
}
void Session_Start(object sender, EventArgs e)
{
Session["test"] = "123";
}
}
}
默认.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string test = Application["test"].ToString();
string test1 = Session["test"].ToString();
}
当程序运行到Page_Load
ofDefault.aspx.cs
时,会抛出异常:System.NullReferenceException
我能做些什么?
解决方案
推荐阅读
- python - 如何在python中使用条件语句查找字母
- api - 使用 Metadata api 读取 Flows
- java - 获取用于注释的 Scala 对象类
- c++ - 如何为新的 cmd.exe 进程创建一个全新的控制台窗口?
- reactjs - 在 React Bootstrap 模式中获取对 React Select 的引用
- bash - 如何从某个日期之前的 .tar 存档中删除文件
- terraform - 对于仅在应用后确定的资源的 terraform 计数是否有解决方法?
- java - Redis 集群与 Spring Boot 集成 - 错误此实例已禁用集群支持
- html - Chrome:尝试在添加的节点中删除 contenteditable 中的空范围
- php - Laravel 8 - 用户表中的两个外键