I would advice against doing that way.
It's actually really easy to get Url Rewriting working.
The hint is to rewrite URL back to original in your page.
A lot of things like search engines and robots rely on correct 404 code
thrown to know that page does not exist. Otherwise it would create infinite
website with the same page given out for any bad URLs. Plus analysing your
logs you will never know that some image is missing or link is broken on
your site.
Url rewriting simplified.
Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string sTmp;
HttpContext ctx = HttpContext.Current;
string sPath = ctx.Request.Path.ToLower();
ctx.Items["OriginalPath"] = sPath;
ctx.RewritePath("~/Default.aspx", "", "" );
}
Page default.aspx
private void Page_Load(object sender, System.EventArgs e)
{
//no caching
Response.CacheControl = "no-cache";
Response.Expires = -1;
HttpContext ctx = Context;
//rewrite URL back.
string sOriginalUrl = (String)ctx.Items["OriginalPath"];
string sOriginalQueryString = (String)ctx.Items["OriginalQueryString"];
if( sOriginalQueryString.Length 0 )
sOriginalQueryString = sOriginalQueryString.Substring(1,
sOriginalQueryString.Length - 1);
ctx.RewritePath(sOriginalUrl, "", sOriginalQueryString);
}
George.
"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:upx7VLI6GHA.3508@TK2MSFTNGP06.phx.gbl...
I have done a bit of research of Url Rewriting, but as yet have been
unsuccessful at getting it to work well, and there are issues around what
file types are supported and how much code you want to write.
While working on a similar, though partially unrelated, issue, I came across
a possible alternate method. Before I pursue this method in full, I wanted
to get opinions of others as to any unforseen issues I might encounter.
The method is this:
In IIS, replace the default 404 error page with a custom ASPX page. Make
sure you don't try to process any errors in the web.config.
When a 404 is hit, the custom ASPX page is called. Although to the user, the
URL they typed in is still visible, the ASPX page is now loaded, and the
QueryString contains the original url typed in.
So if I type in:
http://localhost/some_path
I get redirected to my custom ASPX page with a QueryString containing:
404;
http://localhost:80/some_path
This query string is encoded--so it must be decoded first to be useful. Also
note that it added the port ":80" to the host name.
In my custom ASPX page, I can now parse this url and do my Url Rewriting. If
an unknown url is encountered, I pass to an error page.
To me this is much easier and more elegant than trying to do Url Rewriting
in other ways.
So my questions are:
* Has anyone else tried this, or is currently using this?
* Are there unforseen issues I will encounter with this approach?
--
Greg Collins [Microsoft MVP]
Visit Brain Trove (
http://www.BrainTrove.com )