Skip to main content

ASP.NET, terms, filetypes, examples

http://feeds.delicious.com/rss/learnerplates/httpmodule
http://feeds.delicious.com/rss/learnerplates/httphandler

ASP.NET is the .NET version of ASP, this new version adds more functionality.

ASP and ASP.NET are web development frameworks which allow you the developer to access and manipulate HTML before Posting it up to the client. Both frameworks allow you to access HTML controls (e.g. TextField) using the directive "script runat="server"" . This gives you a reference to the HTML control, that the user sees in their browser, from the server side.
You also have control over the Http Requests and Responses, allowing you to analyze responses sent back, errors and cookies etc can be transmitted over HTTP.

ASP was lacking in that you could not separate the scripting logic from the HTML front end of the file.
ASP.NET allows you to move your logic (written in any .NET language) to a separate file, this is referred to as CodeBehind. The codebehind can be accessed in the aspx file using
<%@ Page Language="C#" CodeFile="AppErr.aspx.cs" Inherits="AppErr" %>
As you can see you must indicate what language you've developed your logic in, C#, the source file which contains your code, AppErr.aspx.cs, and the namespace of the Class, AppErr.
The .cs file can now manipulate and HTML Controls which use the "script runat="server"".
The .cs file is compiled by IIS at runtime and stored, on the Server side, in .NET as MSIL (Microsoft Intermediate Language).
Always remember that the browser can only handle HTML and Script, so no matter what your design is written in, in this case ASP.NET controls, they all render to HTML and Script at runtime.
Here's a description of how a Http Request is processed by ASP.NET (aspalliance)

Lifecycle
The lifecycle of how a HttpRequest/Response is dealt with by ASP.NET (msdn) is pretty long, in short the AppDomain is built up, the Global.asax is compiled and it's Http Handler methods used, the Pages are compiled, the .ashx HttpHandler is compiled and used.

Debugging ASP.NET
Ensure the debug flag in your web.config is set to true. If you are unable to step into the code then edit the file and add the
System.Diagnostics.Debug.Write("Your Debug Message");
If in release mode and you wish the browser to prompt you to start a debug session then use
System.Diagnostics.Debug.Assert(false,"Your debug message");

Exception and Error Handling
There are 3 ways to handle Exceptions/Errors in ASP.NET.
1. Implement a IHttpHandler and name it with .ashx.
2. Implement a IHttpModule and name it with .asax.
3. Override the Page.OnError method in each of your .aspx files.

1. Is the easiest. Create a file name MyApp.ashx, IIS by default associates .ashx files with .aspx files as their error handler. So if an error occurs on a .aspx file it will be redirected to the .ashx file and it's up to you to do whatever you wish here.
The HttpHandler class can be generated in Visual Studio with one of the WebSite Templates. In your IHttpHandler implementation override the ProcessRequest(HttpContext context) method and access the Errors thrown using the context.Error collection.
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/xml";
if (context.Error != null)
{
// Code that runs when an unhandled error occurs
Exception ex = context.Error;
if (ex.GetType().Equals(typeof(HttpException)))
{
string errorString = string.Empty;
errorString = "Http Error: # " + ((HttpException)ex).GetHttpCode().ToString()+ " "+ ex.Message;
context.Response.Write("
}
context.ClearError();
}
}
To test your HttpHandler you'll need to some way to call it , the easiest way is to call the .ashx files url directly in your browser. To get a valid response that the browser can open you'll need to first change the Content type of your ashx page from

context.Response.ContentType = "text/plain";
to
context.Response.ContentType = "text/html";

http://www.15seconds.com/issue/060406.htm

2. HttpModules are classes that Implementet IHttpModule.
HttpModules have methods which are all particular Event Handlers i.e. there is an Event Handler method for the Application_Start and more importantly in for this topic void Application_Error(object sender, EventArgs e) .
You can add your one EventHandler method for application Events in the HttpModules Init method
e.g.
using System;
using System.Web;
public class MyHttpModule : IHttpModule
{

/// In the Init method register for HttpApplication events by adding your handlers
public void Init(System.Web.HttpApplication application)
{
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
application.AuthenticateRequest += new EventHandler(this.Application_AuthenticateRequest);
}

private void Application_BeginRequest(object sender, System.EventArgs e)
{
HttpApplication app = ((HttpApplication)(sender));
HttpContext context = app.Context;
}

private void Application_AuthenticateRequest(object sender, System.EventArgs e)
{
HttpApplication app = ((HttpApplication)(sender));
HttpContext context = app.Context;
}

}

You must then register your HttpModule in the web.config
e.g.
<system.web>
<httpModules>
<add name="MyHttpModuleName" type="MyHttpModule"/>
</httpModules>
What you've to do is create a .asax file, usually called Global.asax, the Global of course meaning this is Application wide, all errors that occur will be caught with this files EventHandler methods. Again the .asax file is mapping in IIS to .aspx pages as an Handler.
Again this file can be created from a Website Template.
Use the HttpApplication.Server object to get the Errors from using the GetLastError() method.


File Types:
.aspx - a file containing the HTML and maybe the scripting language.
.asmx - a proxy class which contains the definition for a webservice.
.asax - a HttpModule, Global.asax, this has handler methods for events which are invoked by the application e.g. Application_start, if you wish to add some handling code for certain events e.g. Exception handling then this is the file, one of the handler methods is Application_Error, just override this method and add append you Response with whatever messages you want.
.ashx - a Handler for the HttpRequests and HttpResponses Debugging HttpHandler (15seconds.com). If you want to examine the contents of the Requests and Responses then this is the place. If you have custom file types, files with your own extensions, then you can create your own Handler file, .ashx, and map it to you file type in your web.config. You can also use the .ashx file to handle your applications errors which have not been handled already, such ass Http 500. In the ProcessRequests method you can call the base,
IHttpHandler.ProcessRequest
for Exceptions if they've occurred then deal with them. Or you can extract all the Errors from the Request,
Context.Request.Error
and process them as you like.

ASP.NET Resources - ASP.NET Custom Error Pages.

Comments

Popular posts from this blog

dotNET - Debugging

Debugging with .NET MSIL assemblies Visual Studio and debugging the CLR are different, I'll talk about both. MSIL Assemblies Assemblies compiled with .NET tools such as the CLR compiler are compiled into a file which contains MSIL (Microsoft Intermediate Language). At runtime the contents of the assembly are loaded into the CLR and ran as machine code. When you compile an assembly in debug a PDB file is generated alongside the DLL or EXE you've just created. The link between these 2 files is that the PDB contains the line numbers of the methods and classes as well as the file names of the original source code that created the assembly. When you launch the debugger in Visual Studio the assembly is loaded into the Debugger (similar to the CLR) along with the PDB file. The debugger now uses your PDB file contents to match the running code found in the assembly to locations in source files (hopefully in your present project). CLR CLR Inside Out (msdn magazine) .NET Framework Tools:...

Installer CustomAction, Debugging the CustomAction, InstallState

Custom Action The Custom Action is added to the Setup Project, select the Project node and hit the Custom Action button. This allows you add an Action to a particular phase in the Installation. But first you must create the Custom Action. To Add a Custom Action you must first have a Custom Action created, this is usually in the form of a Installer Class, this should be created in a seperate project, the Installer Class is actually one of the File Templates in the C# Projects. So it's File->New Project and select Visual C# Projects. Then add a Class Library, this will prompt you for the Class Library Types , select "Installer Class". Walkthrough - Creating Custom Action (msdn). Also here's a more comprehensive document on Setup/Installer implementations, it delves into the Registry etc Getting Started with Setup Projects (SimpleTalk). Visual Studio Setup Projects and Custom Actions (Simple Talk). Create your Installer Class and then add it as a Custom Action to the ...

dotNET - Use app.config ApplicationSettings and UserSettings

When using Settings in an Assembly or .exe you can use the Settings Designer to generate a config file using Settings. The Settings Designer provides a wrapper class which allows you to provide defaults and access the config data using Properties. But what if you're not working inside that Assembly or .exe? this presents a problem. If your loading the Assembly externally and want to access that Assembly's .config file you'll probably wish to use something in the System.Configuration namespace... unfortunately it's not of much use if you've created the .config file from the Settings Designer in Visual Studio!! This is because the Designer creates Sections and ApplicationSettings and UserSettings, the System.Configuration namespace does not provide a method to access these (it has a method to access AppSettings which are a different thing. Below I've written a workaround which locates the app.config and accesses the ApplicationSettings and UserSettings using XML i...