By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,391 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,391 IT Pros & Developers. It's quick & easy.

httpModules :: BeginRequest Behavior

P: n/a
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 "http://www.somesite.com/view/UserName", 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
}

#endregion

private void context_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
if (Regex.IsMatch(app.Context.Request.Url.ToString(), urlmatch))
{
app.Context.RewritePath("someAppropriateHandler.as px");
}
}
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Juan Stang" <gr**@thirdstone.net> wrote in message
news:d2**************************@posting.google.c om...
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 "http://www.somesite.com/view/UserName", 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
}

#endregion

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

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.