Understanding != when rewriting URLs

  • Thread starter Thread starter musosdev
  • Start date Start date
M

musosdev

Hi guys

I have a web app I'm developing in C#. I have a UrlRewrite HTTP Handler,
which seems to work, apart from when I ask it to do something if NOT.

Here's the directory structure...

/default.aspx
/gallery/default.aspx
/gallery/viewartist.aspx

On the main page, I have a link to the gallery section which links to
"/gallery/". From there, I have a number of fake links to artists, e.g.
"/gallery/artist1/", "/gallery/artist2/", etc

The idea is to use URL rewriting to forward those fake links to
"viewartist?artist_id=artist1"

But I still want links to "/gallery/" to work.

Here's the code...

if (url.Contains("/gallery/"))
{
if (url.EndsWith("/gallery/") != true)

waveApp.Context.RewritePath(ConfigurationManager.AppSettings["BaseUrl"] +
"gallery/viewartist.aspx");
}

I don't handle the /gallery/ option, because it has a default.aspx, so
that's sorted.

The problem is, as I've got the code now, it ALWAYS goes to viewartist.aspx.
You can never get to /gallery/default.aspx.

Can someone explain why if (url.EndsWith("/gallery/") != true) seems to get
ignored, and the waveApp.Context.Rewrite... always gets fired?!

Thanks, Dan.
 
Can someone explain why if (url.EndsWith("/gallery/") != true) seems to get
ignored, and the waveApp.Context.Rewrite... always gets fired?!

Is that definitely, definitely your code, exactly as it is? I only ask
because you haven't got braces around the inside statement, which is
okay - unless you've got a rogue semicolon in your actual code.

Personally I'd write it as a single "if" expression:

if (url.Contains("/gallery/") && !url.EndsWith("/gallery/"))
{
waveApp.Context.RewritePath
(ConfigurationManager.AppSettings["BaseUrl"] +
"gallery/viewartist.aspx");
}

Have you stepped in with the debugger to see what's going on?
 
Why not trace the url variable and see what the value is?

When you go to "/gallery/", it ends up going to the default URL which in
this case is "default.aspx". As a result, I bet you are getting
"/gallery/default.aspx" and then processing that, thinking you are getting
"/gallery/".
 
Hi Nicholas/Jon,

John - I originally had the statement as a single IF but it wouldn't work so
I tried it as 2 instead.

In fact, Nicholas' suggestion has solved the problem. I've changed the
second expression to !url.EndsWith("/gallery/default.aspx") and it works fine.

I guess I always thought the URL was the requested URL, and default.aspx
wouldn't have been added yet. Oh well, I've learnt something :)

Thank you both!


Jon Skeet said:
Can someone explain why if (url.EndsWith("/gallery/") != true) seems to get
ignored, and the waveApp.Context.Rewrite... always gets fired?!

Is that definitely, definitely your code, exactly as it is? I only ask
because you haven't got braces around the inside statement, which is
okay - unless you've got a rogue semicolon in your actual code.

Personally I'd write it as a single "if" expression:

if (url.Contains("/gallery/") && !url.EndsWith("/gallery/"))
{
waveApp.Context.RewritePath
(ConfigurationManager.AppSettings["BaseUrl"] +
"gallery/viewartist.aspx");
}

Have you stepped in with the debugger to see what's going on?
 
Back
Top