Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Understanding != when rewriting URLs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
musosdev - 05 Jan 2008 15:55 GMT
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.
Jon Skeet [C# MVP] - 05 Jan 2008 17:14 GMT
<snip>

> 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?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

musosdev - 05 Jan 2008 18:48 GMT
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!

> <snip>
>
[quoted text clipped - 15 lines]
>
> Have you stepped in with the debugger to see what's going on?
Nicholas Paldino [.NET/C# MVP] - 05 Jan 2008 17:16 GMT
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/".

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi guys
>
[quoted text clipped - 38 lines]
>
> Thanks, Dan.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.