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. |
|||
|
|||
16 comments :
hi sir plz solve my problem please
how to implement session expire then page redirect to login page?
nice post...
Sir,How to Save a file after downloading it ?
in the Solution Explorer as well as in the folder in any Local Drive
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
nice tute....its solve my problem
How to access web server controls in app_code class??
hi suresh
can you please provide the code for how to recognise the text in .tiff image without using 3rd party tools(like OCR).
when I run the file global.asax.cs then it show an error could not load type Global
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
the directive application is unknown error this occurring in VS 2013
Pls give me suggestions.....
Really it's amazing..It has solved my problem properly.thanks
Worked for Me. Cheers!! ;)
work for me. thanks.
Almost two days fighting with this type but thanks to you, it solve.. :)
Hi ,
can anyone help me out !!
how can I access other webforms fired methods in global.asax
Note: Only a member of this blog may post a comment.