Visual Studio 2010 中的 global.asax.cs 在哪里

发布于 2024-11-08 12:52:59 字数 152 浏览 0 评论 0原文

我安装的模板中不再有全局应用程序类代码隐藏。我只有 Global.asax。我发现使用 Global.asax.cs 更舒服。

  1. 为什么我再也看不到它了?
  2. 如何重新创建Global.asax.cs?

I don't have a Global Application class code-behind any more inside my installed templates. All I have is Global.asax. I find more comfortable working with Global.asax.cs.

  1. Why am I not seeing it anymore?
  2. How to re-create Global.asax.cs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心凉 2024-11-15 12:53:00

这是因为您创建的是网站而不是 Web 应用程序。我建议您使用预编译的 Web 应用程序模型,但如果您需要使用网站,您可以执行以下操作:

~/Global.asax

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

~/Global.asax.cs:

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

现在在 VS 中重新打开您的站点。

That's because you created a Web Site instead of a Web Application. I would recommend you using a precomipled Web Application model but if you need to use a Web Site you could do the following:

~/Global.asax:

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

~/Global.asax.cs:

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

Now reopen your site in VS.

谎言月老 2024-11-15 12:53:00

是的@Darin的答案是正确的,cs/vb文件可以在Web应用程序中看到,但在网站中你不能有单独的cs/vb文件。

Global.asax没有cs文件,但是你可以编写代码......

<script runat="server">

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

</script>

Yes @Darin's Answer is right, the cs/vb file can be seen in the Web Application but in the website you can't have a separate cs/vb file.

Global.asax doesn't have a cs file, but you can write code....

<script runat="server">

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

</script>
旧时模样 2024-11-15 12:53:00

实际上,在 VS 2010 标准网站中,Global.asax 的设计并不存在代码隐藏文件。那么该怎么办呢?您必须使用这样的内联代码模型。

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

<%@ Import Namespace="System.Web.Routing" %>

<script Language="C#" RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
    }

    void Application_End(object sender, EventArgs e)
    {
    }

    void Application_Error(object sender, EventArgs e)
    {
    }

    void Session_Start(object sender, EventArgs e)
    {
    }

    void Session_End(object sender, EventArgs e)
    {
    }
</script>

Actually in VS 2010 standard website, a code behind file is not present by design for Global.asax. So what to do? You have to use inline code model like this.

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

<%@ Import Namespace="System.Web.Routing" %>

<script Language="C#" RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
    }

    void Application_End(object sender, EventArgs e)
    {
    }

    void Application_Error(object sender, EventArgs e)
    {
    }

    void Session_Start(object sender, EventArgs e)
    {
    }

    void Session_End(object sender, EventArgs e)
    {
    }
</script>
弱骨蛰伏 2024-11-15 12:53:00

按 Ctrl+Shift+A 并从列表中添加全局应用程序类

Press Ctrl+Shift+A and add global application class from the list

稍尽春風 2024-11-15 12:53:00

序言

我不太明白达林的回答,因为他没有遵守全局类命名的标准,恕我直言。我需要一个适用于 VS2005 的解决方案,而不是 2010,但我确信这个解决方案将始终有效。

在 asp.net 2.0 中为 Global.asax 文件添加 CodeBehind

删除任何当前的 Global.asx 文件。 asax 和 Global.cs 文件或尝试修复此问题。

好的,一旦离开,转到项目的根文件夹,右键单击并选择“添加新项目...”

选择“全局类”并单击“确定”

现在再次返回到根文件夹,右键单击并选择一个新的类

名称这个类 - Global.cs

是的 - 允许将其保存在 app_code 文件夹中。不要说“不”,并允许将其放置在根文件夹中。它必须位于app_code 文件夹中。

编辑 Global.asax 文件并剪切(剪切/粘贴)

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

<script runat="server">
  //do not put any code here
</script>

转到 app_code 中的 global.cs 文件并粘贴从 Global.asax 文件中剪切的代码。

app_code 中的 global.cs 文件现在应该如下所示...

/// <summary>
/// Summary description for Global
/// </summary>

public class Global : System.Web.HttpApplication

{

       public Global()

       {

              //

              // TODO: Add constructor logic here

              //

       }



    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.



    }

}

现在我们有了 Global.asax 文件和带有类名的 Global.asax.cs 文件背后的代码。通过这样做,我们可以继续访问网站中任何位置的静态变量。

Preamble

I couldn't quite understand Darin's answer as he's not kept to standards with the naming of the global class, with all due respect. I needed a solution for VS2005, not 2010, but I am sure this solution will work never the less.

Adding CodeBehind for Global.asax file in asp.net 2.0

Delete any current Global.asax and Global.cs files or attempts to fix this.

Ok, once gone, go to the root folder of your project, right-click and choose Add New Item...

Select the Global Class and click OK

Now go back to the root folder again, right-click and choose a new Class

Name this class - Global.cs

And yes - allow it to be saved inside the app_code folder. Do NOT say NO, and allow it to be placed in the root folder. It must be in the app_code folder.

Edit the Global.asax file and cut out (cut/paste) the code inside the <script> tag. The file should look like this, and add the Inherits tag..

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

<script runat="server">
  //do not put any code here
</script>

Go to the global.cs file in app_code and paste the code you cut out of the Global.asax file.

The global.cs file in app_code should now look like this ...

/// <summary>
/// Summary description for Global
/// </summary>

public class Global : System.Web.HttpApplication

{

       public Global()

       {

              //

              // TODO: Add constructor logic here

              //

       }



    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.



    }

}

Now we have Global.asax file and Code behind file Global.asax.cs with a class name. By doing this we can go ahead and access the static variable any where in the web site.

爱本泡沫多脆弱 2024-11-15 12:53:00

这两个文件是相同的“global.asax”与网站相关,“global.asax.cs”与 Web 应用程序相关。两者的编码标准相同

They both files are same "global.asax" relates to website and "global.asax.cs" relates to web application. coding standards are same in both of them

上课铃就是安魂曲 2024-11-15 12:53:00

您不需要创建新的 glabal 库类,因为当您创建新的 Web 应用程序或网站时,您的应用程序本身已经在解决方案资源管理器中具有名为 global.asax.cs 的项目。

You don’t need to create a new glabal library class since when you create new web application or website, your application itself will already have an item named global.asax.cs in your solution explorer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文