473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

httpModules :: BeginRequest Behavior

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
1 2027
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thedotnetteer | last post by:
hi. I wrote a very simple httpmodule to transform calls as www.mysite.com/users/john to www.mysite.com/userspage.aspx?id=john the problem is that my module is called only when the called page...
11
by: Markus Kling | last post by:
Hi, I have a web application that has two sub-applications. The root application defines two httpModules which shall not be loaded for the subapplications. I tried to achieve this by adding ...
0
by: tshad | last post by:
This is just a question on why this is happening. I have an HttpModule that is using the BeginRequest event. I noticed that when I load my page the first time, this event is not called, but if...
0
by: tshad | last post by:
I noticed in my Http modules that all the BeginRequests are handled for each Module before the EndRequests is handled (at least that seems to be the case). I have 2 HttpModules each with...
1
by: Anonieko | last post by:
Global.asax? Use HttpModules Instead! In a previous post, I talked about HttpHandlers - an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are...
2
by: Patrice | last post by:
It looks like we have sometimes a very significant delay (for example 10 s and sometimes more) between the beginrequest and page_load event. It would make me think of some kind of locking issue...
2
by: Mikael Syska | last post by:
Hi, Google gives alot of hits on httpmodules but I can't seem to find any useful on my problem ... I have a site where I'm using my own auth system ... ( guess it could be better but its...
3
by: =?Utf-8?B?Tm9yZW1hYw==?= | last post by:
Hi, We are writing a Web SSO service for all of our websites through Forms Authentication. We also want to provide our websites with the ability to protect different parts of their website and...
6
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi I've created a website in VS2005 with the in-built server, which uses an httpModule for UrlRewriting (using a hook into BeginRequest, doing everything by hand). I've recently transfered...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.