J
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 "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.aspx");
}
}
}
}
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.aspx");
}
}
}
}