January 24, 2007

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.

No comments: