More Questions on Relative URLs and Response.Redirect

  • Thread starter Thread starter daveh551
  • Start date Start date
D

daveh551

Okay, I asked a question a week or so ago asking for an explanation on
relative URL's and the "~" symbol, and several people explained that
the "~" is only usable when the URL is going to be parsed by ASP.NET.

I have a TreeView control with a hierarchical Category listing of
products. Here is the code in selected node changed event:

protected void CategoryTreeView_SelectedNodeChanged(object sender,
EventArgs e)
{
string newSearchCategory =
Server.HtmlEncode(CategoryTreeView.SelectedNode.ValuePath);
LogEvents.LogInfo("Category Redirect Location: \"~/
DisplayCategory.aspx?category=" + newSearchCategory + "\"");
Response.Redirect("~/DisplayCategory.aspx?category=" +
newSearchCategory);
}

First of all, from the docs I got the impression that the HtmlEncode
call would convert all the spaces and special characters into hex so
they wouldn't interfere with the redirection, but the Log file shows
that they aren't being converted:

7/30/2008 4:27:38 PM INFO Category Redirect Location: "~/
DisplayCategory.aspx?category=Jewelry with Other Semi-Precious
Stones:Amethyst Jewelry"
7/30/2008 4:27:53 PM INFO Category Redirect Location: "~/
DisplayCategory.aspx?category=Jewelry with Other Semi-Precious Stones"

But my main problem is that, if there is a colon character (which I am
using as a path separator) in the query string, the Response.Redirect
is not parsing the "~", but passing it through to IIS, and I'm getting
a "Resource Not Found" exception, with "Requested URL:"/OBS/~/
DisplayCategory.aspx" The first example in the log excerpt above,
where "Amethyst Jewelry" is a subcategory of "Jewelry with Other Semi-
Precious Stones" fails. The second one, where there is no subcategory
and no colon, succeeds.

Is that a known behavior? Is there another character that I can use
instead of ":" that will avoid the behavior (I don't want to use '/',
because that can occur within the category name.) Or is there some
other method than HTMLEncode that will properly hide the special
characters?

Thanks for your help.
 
Have you tried the ResolveUrl function?

That way you could do:
Response.Redirect(ResolveUrl("~/DisplayCategory.aspx"));

for querystrings I just do:
Response.Redirect(ResolveUrl("~/DisplayCategory.aspx") +
"?myval=somevalue");

you can pass querystrings to the resolveurl method but I like to play it
safe.

Now, something else. Do you realize your query strings are illegal?
DisplayCategory.aspx?category=Jewelry with Other Semi-Precious Stones
This is not a valid querystring. Spaces are an illegal character. Some
browsers may let you get away with it because they'll automatically replace
the spaces with %20, the url code for spaces. Instead, use the Server.
UrlEncode method to make the querystring valid.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 
Have you tried the ResolveUrl function?

That way you could do:
Response.Redirect(ResolveUrl("~/DisplayCategory.aspx"));

for querystrings I just do:
Response.Redirect(ResolveUrl("~/DisplayCategory.aspx") +
"?myval=somevalue");

you can pass querystrings to the resolveurl method but I like to play it
safe.

Now, something else. Do you realize your query strings are illegal?
DisplayCategory.aspx?category=Jewelry with Other Semi-Precious Stones
This is not a valid querystring. Spaces are an illegal character. Some
browsers may let you get away with it because they'll automatically replace
the spaces with %20, the url code for spaces. Instead, use the Server.
UrlEncode method to make the querystring valid.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

Mark,

THANK YOU so much. It's the UrlEncode function that I wanted. I was
using the HtmlEncode by mistake. When I replace HtmlEncode with
UrlEncode (and the corresponding decode function), the problem goes
away.

I'll check out ResolveUrl for future use. I'm learning something with
every mistake (but it sure is a long process!)
 
Back
Top