Global.asax.cs 文件在哪里? [英] Where is the Global.asax.cs file?

查看:21
本文介绍了Global.asax.cs 文件在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 VS 2008.我从 File->New->Website->Asp.net Website 创建了一个新的 Asp.net 网站项目.

I am using VS 2008. I have created a new Asp.net web site project from File->New->Website->Asp.net Website.

现在我想将 Global.asax 和 .cs 文件添加到项目中.所以我右键单击项目->添加新项目->全局应用程序类.然后我点击了添加按钮.

Now I want to add the Global.asax as well as the .cs file to the project. So I Right click on the project ->Add New Item->Global Application Class. Then I clicked on the add button.

添加了 Global.asax 文件,内容如下

The Global.asax file got added with the content as under

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

 <script runat="server"%>

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

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

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>

这很好.但是 Global.asax.cs 文件在哪里?

This is fine. But where is the Global.asax.cs file?

谢谢

推荐答案

那是因为您创建的是网站而不是 Web 应用程序.cs/vb 文件只能在 Web 应用程序中看到,但在网站中你不能有单独的 cs/vb 文件.

That's because you created a Web Site instead of a Web Application. The cs/vb files can only be seen in a Web Application, but in a website you can't have a separate cs/vb file.

在网站中,您可以添加一个 cs 文件行为,例如..

In the website you can add a cs file behavior like..

<%@ Application CodeFile="Global.asax.cs" Inherits="ApplicationName.MyApplication" Language="C#" %>

~/Global.asax.cs:

namespace ApplicationName
{
    public partial class MyApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
        }
    }
}

这篇关于Global.asax.cs 文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆