httpModules :: BeginRequest Behavior


Juan Stang

I'm trying to implement some kind of page controller, a class that
will intercept all incoming requests and route them appropriately.
More specifically, I'd like to intercept a request for a non-existent
url, say "", and have that
request URL parsed so that I could route all URLs containing "view" to
the appropriate form which would fulfill the request with "UserName"
as a parameter.

I've figured that a HttpModule is what I want, since I'm trying to
filter requests, and in particular I want to be able to handle
non-existent URLs. I wrote the code for a handler that did a
Regex.IsMatch on the request URL looking for my desired keyword,
"view" for example, and it works, kind of. If the <i>handler</i>
exists, then the following app.Context.RewritePath does the job,
successfully rewriting the URL.

But if the handler does not exist, I get a 404!!! Argh! Isn't the
BeginRequest EventHandler supposed to handle requests before they go
looking for the handler? What am I doing wrong!!!???!!!??? Any help
would be appreciated. Sample code is pasted below.


using System;
using System.Text.RegularExpressions;
using System.Web;

namespace somenamespace
/// <summary>
/// Summary description for Controller.
/// </summary>
public class Controller : IHttpModule
private String urlmatch = "view";

public Controller()

#region IHttpModule Members

public void Init(HttpApplication context) {
context.BeginRequest += new EventHandler(context_BeginRequest);

public void Dispose() {
// TODO: Add Controller.Dispose implementation


private void context_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
if (Regex.IsMatch(app.Context.Request.Url.ToString(), urlmatch))



Manohar Kamath [MVP]

You may have to set the 404 page at the IIS level to go to an aspx page, and
handle all non-esistent ones here. The handler and the module are only for
those pages that end with .aspx extension.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question