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.
- Why am I not seeing it anymore?
- How to re-create 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.
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.
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>
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>
Press Ctrl+Shift+A and add global application class from the list
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.
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
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.