Library tutorials & articles
Implementing HTTP Handlers in ASP.NET
- Introduction
- Creating the Handler
- Handling PostBack
- Redirect, Transfer or Rewrite?
Creating the Handler
One of the most important things to implementing your HTTP handler is the management of your URL mappings. Before you look at how the handler should be coded, you should put some thought into how flexible you want the mappings to be. There are countless methods for managing your mappings, each with its own set of pros and cons. For instance, you could technically put them in a database; which would allow you to setup a nice front-end to manage them from within your application. The problem with this is that you'll require a database call simply to find out what page you want to access. This may or may not be adequate. I would assume that the latter would be true in most situations. You should also consider the fact that, in some cases, you may require more than one rewrite or redirect in order to setup your mappings appropriately. For this article, I will keep it very simplistic. We will use the custom app settings section available within the Web.config file. To do this, add the following section to your Web.config file:
Web.config appSettings Configuration
<
appSettings
>
<add
key="/MyApp/LogicalPage1.aspx"
value="~/Pages/PhysicalPage1.aspx" />
<add
key="/MyApp/LogicalPage2.aspx"
value="~/Pages/PhysicalPage2.aspx" />
</appSettings>
The key is intended to be the requested page and the value
is the physical page that will be displayed. Pretty simple. Two important
things to note are that, using this simplified scenario, the keymust be and the value should be root-relative paths.. For
instance, the above specifies that http://localhost/MyApp/LogicalPage1.aspx
will actually map to http://localhost/MyApp/Pages/PhysicalPage1.aspx.
Now that we've defined our mappings, I recommend that you create a configuration settings reader to load and act upon the appropriate mapping at runtime. For this example, I won't get into that, though. This simple implementation only requires a one-line lookup, so there is not much of a need to have the settings reader; however, in a real-world app, I would highly suggest using one for extensibility reasons. I will discuss this more in-depth later.
Creating the HTTP Handler
Now that we have decided on our mapping storage method and have ensured a way to
read the mappings (built-in configuration support for now), all we have to do
is create the HTTP handler. There are a lot of different ways to do this, so
the first thing to think about is: What do you want to do? For this article,
we're just rewriting the URL, but for your system, you might want to add
application-level logic. If this is the case, I recommend that you create
special business objects to handle each logical task that needs to be
accomplished. For instance, a LogAction class for logging or a RewriteUrl
class for the URL rewriting. Since we will only be implementing a simple URL
rewrite, I won't bother getting into the complexities of a separate class.
Before you set forth with creating your HTTP handler, you should take a look at
the IHttpHandler interface, which you will need to implement.
IHttpHandler Interface
public interface IHttpHandler
{
bool IsReusable { get;
}
void ProcessRequest(HttpContext context);
}
There is one property and one method to implement. The property, IsReusable,
specifies whether ASP.NET should reuse the same instance of the HTTP handler
for multiple requests. My thinking is that, unless there is a specific reason
not to, you would always want to reuse the HTTP handler. Unfortunately, I
haven't found any guidance suggesting one way or another - at least, not with
any real reasoning behind it. The only thing I found was something to the
effect of, unless your handler has an expensive instantiation, set IsReusable
to false.
The ProcessRequest() method is where you will actually perform the
logic to handle the request. Since we're simply reading from the app settings
and rewriting the URL, we can handle this in a matter of lines.
HttpHandler.ProcessRequest() Method
public void ProcessRequest(HttpContext context)
{
// declare vars
string requestedUrl;
string targetUrl;
int urlLength;
// save requested, target url
requestedUrl = context.Request.RawUrl;
if ( requestedUrl.IndexOf("?") >= 0 )
targetUrl =
ConfigurationSettings.AppSettings[requestedUrl.Substring(0,
requestedUrl.IndexOf("?"))];
else
targetUrl = ConfigurationSettings.AppSettings[requestedUrl];
if ( targetUrl == null
|| targetUrl.Length == 0 )
targetUrl = requestedUrl;
// save target url length
urlLength = targetUrl.IndexOf("?");
if ( urlLength == -1 )
urlLength = targetUrl.Length;
// rewrite path
context.RewritePath(targetUrl);
IHttpHandler handler = PageParser.GetCompiledPageInstance(
targetUrl.Substring(0, urlLength), null,
context );
handler.ProcessRequest(context);
}
Now, all we need to do is add the HTTP handler reference in the Web.config file.
A lot of people have been falling victim to the following Server.Transfer()
error because of incorrect handler configurations, so pay attention to this
part.
Error executing child request for [physical page specified in appSettings value].aspx
I'll discuss the reasoning behind the following configuration, but for now,
simply replace "*/Pages/*.aspx" with an appropriate path that
represents all of the physicalpages (this is veryimportant), MyApp.HttpHandler with the fully-qualified class
path of the HTTP handler, and MyApp with the name of the assembly,
minus the .dll extension. Also note that the handler for the physical pages must
come first. These handlers are checked in order, so if you put it second,
then the first path that the request matches will be used, which
will probably be your custom handler.
Web.config system.web/httpHandlers Configuration
<system.web>
<httpHandlers>
<add
verb="*"
path="*/Pages/*.aspx"
type="System.Web.UI.PageHandlerFactory"
/>
<add
verb="*"
path="*.aspx"
type="MyApp.HttpHandler,MyApp"
/>
</httpHandlers>
</system.web>
Related articles
Related discussion
-
Gizmox Announces release of Visual WebGui SDK version 6.2.2
by Visual WebGui (0 replies)
-
Error 4 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
by lbargers (3 replies)
-
How to receive data in web server sending from GPRS modem
by AshokSingh (1 replies)
-
Working with frames in c# .net
by pulak2008 (0 replies)
-
Time Out Problem
by lorrainepetty (1 replies)
Related podcasts
-
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
.net developer
in Rijswijk (€2K-€4K per annum)
Events coming up
-
Dec
3
An afternoon of SQL Server Data Services and ASP.NET Dynamic Data
Bradford, United Kingdom
This event is in association with Black Marble. In the morning Black Marble will be presenting on Microsoft "Oslo": The Future of Enterprise Applications. To find out more about this please follow the link on the right.
I haven't touched this since .NET 1.1. And, with IIS7, I'd say there's a better approach. I'll probably revisit this when I have some time to play with IIS7.
Did you ever find a better solution for the ActionLess form - for Url Rewriting ???
Your post to experts exchange requires a subscriptin to read the answer...
Cant I just get away with in NET 2.0 putting form id='myForm' Method=""
I know it is malformed ...
You seem to have forgotten an important problem with your actionless form. I have discovered that this custom form will disable the validation controls. Now the onsubmit function for client side validation needs to be added back in. I found this post about the problem (http://www.experts-exchange.com/Web/Q_21842631.html), but am still wondering if there is not an easier way to fix this.
thank you very much. I am knew this posting and waiting for nothoing
This thread is for discussions of Implementing HTTP Handlers in ASP.NET.