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;
namespace MyNamespace
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
}
}