Clean display URL on custom error redirect

C

cpnet

I've figured out how to use the <customErrors/> element in my web config
file to to redirect 404 errors to a custom error handler. But, it displays
an 'ugly' URL in the client's browser. For example, if someone browses to:

www.mydomain.com/Users/BobJones/

then my app should display a page with info about Bob Jones.
www.mydomain.com/Users/ is a real folder on my website that only contains a
single file (default.aspx). I've configured my app so that when the user
browses to www.mydomain.com/Users/BobJones/, a 404 error will be caused, and
will redirect the user to www.mydomain.com/Users/Default.aspx. The
Default.aspx page knows that the user was actually trying to go to
www.mydomain.com/Users/BobJones/, so Default.aspx displays info about Bob
Jones. All of that is working fine.

The problem though is that when the user tries to browse to
www.mydomain.com/Users/BobJones/, the actual URL that shows up in IE is:

www.mydomain.com/Users/Default.aspx?aspxerrorpath=/Users/BobJones

How can I get IE to simply display, www.mydomain.com/Users/BobJones/, in the
address bar (even though the content is being generated by
www.mydomain.com/Users/Default.aspx)?

Thanks.
 
C

Charles

Redirect away on the custom error page? ie Redirect to default.aspx on
load. Wouldn't this work ?
 
C

Charles

Okay, so are you Rewriting the path on begin request? I have done
something similar before. You could try rewriting the path again on the
begin request for the error page. Just check that they are going to the
error page, then if they are, read the aspxerrorpath from the query,
and rewrite the path using this value. that way you can have a nice
generic solution that you can use in your other sites. Are you by
chance developing a marketing system for your content so everything has
its own nice categorized folder path?

Charles
 
C

cpnet

I am trying to build a system so that links are always to a nice folder path
instead of links that use query strings. (My site will have dynamically
generated paths).

I'm not sure how to rewrite the path, but I think I figured out how to solve
the problem.

Just to recap, www.mydomain.com/users/ is a real path in my project.
www.mydomain.com/users/BobSmith/ is not (there's no "BobSmith" folder on my
server. If a user tries to browse to www.mydomain.comi/users/BobSmith, I
want that URL to stay displayed in their IE address bar, but I want the
actual content they see to be produced by the .aspx page at
www.mydomain.com/users/Default.aspx.

I tried using the customErrors element in my web.config file to create a
redirect for 404 errors. It almost works - the user sees the content
produced by www.mydomain.com/users/Default.aspx, but the path in the IE
address bar is
www.mydomain.com/users/Default.aspx?aspxerrorpath=/Users/BobJones.

I found that if instead of using the customErrors in my web.config file, I
just set up a custom error (for error 404) in IIS, then the URL displayed in
the user's browser is the desired, www.mydomain.com/users/BobSmith/, but the
content is created by www.mydomain.com/users/default.aspx. So the moral of
the story in this case seems to be to not use the web.config file to set up
custom error handling - I have to use IIS for this instead.

But, if there is a way to do what I want using the web.config file
customErrors element, I'd love to know how.
 
C

cpnet

I found out the IIS approach has some extra restrictions... My 404 handler
page (the page that shows the content for "BobSmith" usese master pages, and
the ASP.NET theming. In the rendered page, all of the stylesheets and pics
are referred to with relative links. The links to the style sheets etc. are
rendered to HTML in relation to the actual location of my .aspx page. But
IE, expects to find these objects relative to the URL displayed in the
address bar. So, the URL the user is typing is:

www.mydomain.com/users/BobSmith/

So, I have to put the error handling page in a subfolder of
www.mydomain.com/users/ (I'm using
www.mydomain.com/users/handler/Default.aspx). That way a relative link in
Default.aspx like, '../../stylesheet.css' goes to the same place when viewed
from the perspective of www.mydomain.com/users/handler/ or
www.mydomain.com/users/BobSmith/.

I can't help but think though that there's an easier way.
 
C

cpnet

With your mention of "rewriting" I had a new term to google on. And yes,
that is exactly what I am trying to do! After a lot of frustration, I have
settled on implementing an IHTTPModule to handle the 'fake' directories. I
have also found that by using the HttpContext.RewritePath overload like:

context.RewritePath("~/Users/Default.aspx?userid=1", false);

I don't have the problems with relative links. (It's passing 'false' as the
second argument that does the trick). The documentation on this is pretty
slim so I have no idea why this works, but it just does.
 
C

Charles

Thats the trick. You could use Lutz Decompiler to see what the function
does. Just think of it as a redirect without sending the redirect
header to the client. You coudl get slick and database all of the paths
to content, and pass the id of the content to the new page. Good luck,
happy coding,

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top