Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to add Global.asax.cs file in asp.net or how to add Code behind file for Global.asax in asp.net

May 19, 2011
Introduction

Here I will explain how to add code behind file for Global.asax in asp.net.

Description:

In previous post I explained clearly about Global.asax file uses and events in asp.net. Here I will explain how to add code behind file to Golbal.asax in asp.net. In one of my project I started writing code in Global.asax file but we decided to write code in Global.asax.cs file code I tried to search for that one but that is not available in our application at that I learn one thing that is we need to add Global.asax and Global.asax.cs these files to our application. For that one first right click on solution explorer and select Add New item


After that select Global Application class file and click ok now Global.asax file added successfully to our application


After add our Global.asax file to application that would be like this


And our Global.asax file contains code like this

<%@ 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.
}
</script>
Now our requirement is to add Global.asax.cs file to our code for that again right click on solution explorer and select Add new item another wizard will open in that select Class file and give name as Global.asax.cs and click Add


After click Add that will show one warning message like this now click yes


After adding our Global.asax.cs file we need make some changes to use Global.asax.cs file those are first remove entire script code from your Global.asax file

<script runat="server">
---------------------------
---------------------------
---------------------------
</script>
And change this line in Global.asax file to

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

<%@ Application Language="C#" CodeBehind="~/App_Code/global.asax.cs" Inherits="Global" %>
Now open Global.asax.cs file and change this like

public class Global
To

public class Global:System.Web.HttpApplication

Now write the events in Global.asax.cs file and check with break point it will work for you

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

16 comments :

Sikandar said...

hi sir plz solve my problem please


how to implement session expire then page redirect to login page?

Sivaprakash.S said...

nice post...

Sunil Pal said...

Sir,How to Save a file after downloading it ?

in the Solution Explorer as well as in the folder in any Local Drive

Unknown said...

I am using global.asax file in my web site.

some times Session_Start event in global.asax
fires on each page request why woul this happen?

please solve my prblm

Anonymous said...

nice tute....its solve my problem

Anonymous said...

How to access web server controls in app_code class??

ramya said...

hi suresh

can you please provide the code for how to recognise the text in .tiff image without using 3rd party tools(like OCR).

Anonymous said...

when I run the file global.asax.cs then it show an error could not load type Global

Devaneios said...
This comment has been removed by the author.
Anonymous said...

I could kiss you right now. Thank you so much...

FYI - Came here through http://stackoverflow.com/questions/6055927/where-is-global-asax-cs-in-visual-studio-2010

Gaurav said...

the directive application is unknown error this occurring in VS 2013
Pls give me suggestions.....

Neha said...

Really it's amazing..It has solved my problem properly.thanks

Anonymous said...

Worked for Me. Cheers!! ;)

Anonymous said...

work for me. thanks.

Anonymous said...

Almost two days fighting with this type but thanks to you, it solve.. :)

Anonymous said...

Hi ,
can anyone help me out !!
how can I access other webforms fired methods in global.asax

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.