Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OK, so I am biting the bullet and trying to use LINQ more. I am creating a web app where I'd like the application object to store a small amount of static data. It's stored in an XML file. Trouble is, I can't get my global.asax file to use LINQ, because I can't put a using statement anywhere inside it without getting an error, which means I can't use the basic LINQ syntax.

XML
<script runat="server">

    using System.Linq;

    void Application_Start(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/appdata/breeds.xml");
    }


This gives me an error.

Looks like my real question is, how do I create a global.asax file with a code behind ? The IDE won't do it, and doing it by hand is not working for me, I get a 'Could not load type 'MyNamespace.Global' error.

My Global.asax:

<%@ Application Codebehind="Global.asax.cs" Inherits="DIA.Global" Language="C#" %>

The global.asax.cs starts with:

C#
using System;
using System.Web;

namespace DIA
{
    public class Global : HttpApplication
    {
Posted
Updated 10-Oct-10 15:10pm
v3

OK, I did some playing around and found another way of doing it...

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


global.asax.cs (in root of project, not App_Code)
C#
using System;
using System.Web;
//using's here

public partial class Global : HttpApplication
{
    /*code goes here... */
}


According to this[^], CodeBehind is deprecated and CodeFile is the correct attribute.

In VS2010, CodeFile isn't even in the intellisense attribute list inside <%@Application %> - at least for me - which is why my first example used CodeBehind. That is the stupid part about this issue I guess. I remembered that there was another newer way, hence this answer...
 
Share this answer
 
v5
After creating a fresh ASP.Net web application, I created the global.asax file by right clicking the project, selecting add "New Item..." and selecting "Global Application Class". I don't think it gives you that option after you already have a global.asax file, so it's important to do it before you tinker with stuff. Here is the code behind it gave me by default:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace WebApplication2
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
        }
        protected void Session_Start(object sender, EventArgs e)
        {
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }
        protected void Application_Error(object sender, EventArgs e)
        {
        }
        protected void Session_End(object sender, EventArgs e)
        {
        }
        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}

And the contents of the global.asax file:
ASP.NET
<%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication2.Global" Language="C#" %>

Notice that LINQ is included by default (also note that CP decided to take away some of the blank lines... so the file doesn't appear exactly as it did when I copied it).
 
Share this answer
 
Comments
AspDotNetDev 10-Oct-10 21:36pm    
Note that I'm using Visual Web Developer Express 2008.
Christian Graus 10-Oct-10 21:43pm    
Odd, I am using VS2008 and I created two new web apps, and added Global.asax both times and both times the option for a code behind file was disabled.
First of all, you can use an equivalent of using statements without codebehind:
<%@ Import Namespace="System.Linq" %>

...however I personally prefer the codebehind style because it works the same way as normal C# code, and has greater flexibility.

Converting to codebehind



When you create a project via File->New->Website, VS will create a website where the auto-generated default items are in the non-codebehind/"scripty" style of ASP.NET coding. If you prefer using the codebehind style, you can still easily convert to using the codebehind arrangement for Global.asax, either by deleting or re-adding, or by following these steps:
1. Add a class file the normal way, called Global.asax.cs - if you created your site via the Website template, make sure your class file goes in App_Code.
2. Make the class inherit from HttpApplication.
3. Move your code into the class file, and add the Codebehind and Inherits attributes to <%@Application %> in Global.asax, as I show in my samples below.

If you create a website by using File->New->Project/Solution and selecting one of the Web Application templates, the default files will be generated via the codebehind coding style. This is what I generally prefer.

Sample codebehind global.asax



Here is a sample of what a codebehind-style global.asax and global.asax.cs would look like:

global.asax
<%@ Application Codebehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>


global.asax.cs
using System;
using System.Web;
//as many using's go here as you please ;)

namespace MyNamespace
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            //code here...
        }
        
        //other events and methods...
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900