RewritePath method not working correctly
I'm running a the following code snippet in Global.asax:
public void Application_BeginRequest(object sender, EventArgs e)
{
string currentPath;
string customPath;
currentPath = Request.Path.ToLower();
if(currentPath.IndexOf("/news/") > 0 &&
System.IO.Path.GetExtension(currentPath) == ".aspx")
{
/*
the request is within the /NEWS/ directory, so re-write the URL
to appear to be a filename, rather than a URL with a query string.
==================================================
user-visible URL: http://www.domain.com/news/1950.aspx
actual URL: http://www.domain.com/news/story.aspx?headline=1950
==================================================
*/
customPath =
String.Format("/news/story.aspx?headline={0}",System.IO.Path.GetFileNameWith
outExtension(Request.Path));
Context.RewritePath(customPath);
}
}
Essentially, it's disguising URLs to make things a little easier to
remember. I use thie by placing several anchor tags <a href...> on a page
that point to a URL like http://www.domain.com/news/4578.aspx and the
proper URL is loaded in the background, and the user is none the wiser.
However, it's not working as it should. On my production machine at home,
it does as planned, but browsing to a URL like
http://www.domain.com/news/4578.aspx gets a 404 error, thinking the file is
actually one on disk. In short, it tries to browse directly for the file
Does it matter if the server I'm on is running ASP.NET 1.0 server or 1.1?
I'm using 1.0 on my home development machine and my host is using 1.1. Or
does it matter, meaning both are installed and my code should work with
either? I read somewhere that the RewritePath method is a bit different in
1.1 than 1.0, the latter of which my site is written in.
Thanks!
Jas
Re: RewritePath method not working correctly
Got this one working. :)
"Jason Salas" <jason@kuam.com> wrote in message
news:3f3f94cd@tnews.web.devx.com...
> I'm running a the following code snippet in Global.asax:
>
> public void Application_BeginRequest(object sender, EventArgs e)
> {
> string currentPath;
> string customPath;
>
> currentPath = Request.Path.ToLower();
> if(currentPath.IndexOf("/news/") > 0 &&
> System.IO.Path.GetExtension(currentPath) == ".aspx")
> {
> /*
> the request is within the /NEWS/ directory, so re-write the
URL
> to appear to be a filename, rather than a URL with a query string.
> ==================================================
> user-visible URL: http://www.domain.com/news/1950.aspx
> actual URL:
http://www.domain.com/news/story.aspx?headline=1950
> ==================================================
> */
> customPath =
>
String.Format("/news/story.aspx?headline={0}",System.IO.Path.GetFileNameWith
> outExtension(Request.Path));
> Context.RewritePath(customPath);
> }
>
> }
>
> Essentially, it's disguising URLs to make things a little easier to
> remember. I use thie by placing several anchor tags <a href...> on a page
> that point to a URL like http://www.domain.com/news/4578.aspx and the
> proper URL is loaded in the background, and the user is none the wiser.
> However, it's not working as it should. On my production machine at home,
> it does as planned, but browsing to a URL like
> http://www.domain.com/news/4578.aspx gets a 404 error, thinking the file
is
> actually one on disk. In short, it tries to browse directly for the file
>
> Does it matter if the server I'm on is running ASP.NET 1.0 server or 1.1?
> I'm using 1.0 on my home development machine and my host is using 1.1. Or
> does it matter, meaning both are installed and my code should work with
> either? I read somewhere that the RewritePath method is a bit different
in
> 1.1 than 1.0, the latter of which my site is written in.
>
> Thanks!
> Jas
>
>
Re: RewritePath method not working correctly
> Got this one working. :)
What was the problem?
--
Phil Weber
Re: RewritePath method not working correctly
The IndexOf method in the Application_BeginRequest for some reason was
ignoring the first forward slash in "/news/", and returning and value that
caused my conditional statement to be false. That's weird, seeing as it
worked as such at home. :)
"Phil Weber" <philweber@hotmail.com> wrote in message
news:3f404a07$1@tnews.web.devx.com...
> > Got this one working. :)
>
> What was the problem?
> --
> Phil Weber
>
>