Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Extracting path from URL?

Extracting path from URL?

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasesql-server
18 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J John Arlen

    System.IO.Path class offers several static functions that work on both file system paths as well as URLs. The one that addresses your question is: "http://samplesite.com/account" == Path.GetDirectoryName("http://samplesite.com/account/myaccount.aspx");

    M Offline
    M Offline
    mittalpa
    wrote on last edited by
    #3

    This is how it will will be done. Dim strFolder as String strFolder = Left(Request.Url.AbsoluteUri, InStrRev(Request.Url.AbsoluteUri, "/")) Enjoy .. Follow your goals, Means will follow you ---Gandhi---

    N B 2 Replies Last reply
    0
    • M mittalpa

      This is how it will will be done. Dim strFolder as String strFolder = Left(Request.Url.AbsoluteUri, InStrRev(Request.Url.AbsoluteUri, "/")) Enjoy .. Follow your goals, Means will follow you ---Gandhi---

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #4

      Curious, what was wrong with the previous answer?

      M 2 Replies Last reply
      0
      • N Not Active

        Curious, what was wrong with the previous answer?

        M Offline
        M Offline
        mittalpa
        wrote on last edited by
        #5

        I think the functions in this class are mainly for Windows File System. It was returning "http:samplesite.comaccount". For handling URL's, I would prefer the way I did. Request.Url is meant to for such URL parsing. Follow your goals, Means will follow you ---Gandhi---

        N 1 Reply Last reply
        0
        • N Not Active

          Curious, what was wrong with the previous answer?

          M Offline
          M Offline
          mittalpa
          wrote on last edited by
          #6

          I think the functions in this class are mainly for Windows File System. It was returning "http:samplesite.comaccount". For handling URL's, I would prefer the way I did. Request.Url is meant for such URL parsing. Follow your goals, Means will follow you ---Gandhi---

          1 Reply Last reply
          0
          • M mittalpa

            I think the functions in this class are mainly for Windows File System. It was returning "http:samplesite.comaccount". For handling URL's, I would prefer the way I did. Request.Url is meant to for such URL parsing. Follow your goals, Means will follow you ---Gandhi---

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #7

            Correct they are for Windows File Systems and unless I'm missing something that is what you are using. Using your method is inherently dangerous. Are you checking for valid uri before getting it or just hoping it is valid when parsed? The previous method uses built in exception handling of the class. What's the point of having a tool if you're not going to use it?

            B 1 Reply Last reply
            0
            • M mittalpa

              This is how it will will be done. Dim strFolder as String strFolder = Left(Request.Url.AbsoluteUri, InStrRev(Request.Url.AbsoluteUri, "/")) Enjoy .. Follow your goals, Means will follow you ---Gandhi---

              B Offline
              B Offline
              Blake Coverett
              wrote on last edited by
              #8

              You need to use AbsolutePath instead of AbsoluteUri. Try the perfectly valid "http://foo.com/bar.htm?baz=x/y" as an test case to see the difference. -Blake

              N M 2 Replies Last reply
              0
              • N Not Active

                Correct they are for Windows File Systems and unless I'm missing something that is what you are using. Using your method is inherently dangerous. Are you checking for valid uri before getting it or just hoping it is valid when parsed? The previous method uses built in exception handling of the class. What's the point of having a tool if you're not going to use it?

                B Offline
                B Offline
                Blake Coverett
                wrote on last edited by
                #9

                Mark Nischalke wrote: Correct they are for Windows File Systems and unless I'm missing something that is what you are using. Yes, you are missing something. mittalpa is correct. He is parsing a URL, not a file system path. They are not the same and code for one will not handle the other correctly. Mark Nischalke wrote: Using your method is inherently dangerous. Are you checking for valid uri before getting it or just hoping it is valid when parsed? The previous method uses built in exception handling of the class. What's the point of having a tool if you're not going to use it? No, his method is not dangerous. He is parsing the uri from the Request object, not some arbitrary one passed in from elsewhere, so yes, it has already been canonicalized and validated. (His code wouldn't be running if it weren't.) -Blake

                N 1 Reply Last reply
                0
                • B Blake Coverett

                  You need to use AbsolutePath instead of AbsoluteUri. Try the perfectly valid "http://foo.com/bar.htm?baz=x/y" as an test case to see the difference. -Blake

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #10

                  This proves my point from above. No checks were made for this possibility whereas the System.IO method ignores the query string.

                  1 Reply Last reply
                  0
                  • B Blake Coverett

                    Mark Nischalke wrote: Correct they are for Windows File Systems and unless I'm missing something that is what you are using. Yes, you are missing something. mittalpa is correct. He is parsing a URL, not a file system path. They are not the same and code for one will not handle the other correctly. Mark Nischalke wrote: Using your method is inherently dangerous. Are you checking for valid uri before getting it or just hoping it is valid when parsed? The previous method uses built in exception handling of the class. What's the point of having a tool if you're not going to use it? No, his method is not dangerous. He is parsing the uri from the Request object, not some arbitrary one passed in from elsewhere, so yes, it has already been canonicalized and validated. (His code wouldn't be running if it weren't.) -Blake

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #11

                    Add a couple of slashes to the address used to get here. Still gets you here but may not parse correctly if you are only expecting one. Blake Coverett wrote: They are not the same and code for one will not handle the other correctly System.IO.Path does handle it.

                    B 2 Replies Last reply
                    0
                    • B Blake Coverett

                      You need to use AbsolutePath instead of AbsoluteUri. Try the perfectly valid "http://foo.com/bar.htm?baz=x/y" as an test case to see the difference. -Blake

                      M Offline
                      M Offline
                      mittalpa
                      wrote on last edited by
                      #12

                      you broke my code Blake..:(( "http://samplesite.com/account/myaccount.aspx?abc=x/y" anywho.. if I use AbsolutePath instead of AbsoluteUri, I get "/account/myaccount.aspx". What about "http://samplesite.com" ? How do get that? Follow your goals, Means will follow you ---Gandhi---

                      B 1 Reply Last reply
                      0
                      • N Not Active

                        Add a couple of slashes to the address used to get here. Still gets you here but may not parse correctly if you are only expecting one. Blake Coverett wrote: They are not the same and code for one will not handle the other correctly System.IO.Path does handle it.

                        B Offline
                        B Offline
                        Blake Coverett
                        wrote on last edited by
                        #13

                        Mark Nischalke wrote: Add a couple of slashes to the address used to get here. Still gets you here but may not parse correctly if you are only expecting one. Wrong. Try it and you will see for yourself. As I already told you, System.Uri class canonicalized the URL. If I use http://foo.com/bar///test.aspx, yes I will get the same page as http://foo.com/bar/test.aspx, but the value of Request.Url.AbsolutePath will be /bar/test.aspx with the extra slashes removed in both cases. Mark Nischalke wrote: System.IO.Path does handle it. System.IO.Path is for file system paths not URI paths. The two are not the same. Valid instances of one are not always valid instances of the other. In particular, System.IO.Path knows nothing about query strings or fragment identifiers. If you pass a valid Uri containing those into System.IO.Path you will end up with either an exception or the wrong result. Check your facts before you spout off next time. -Blake

                        N 1 Reply Last reply
                        0
                        • N Not Active

                          Add a couple of slashes to the address used to get here. Still gets you here but may not parse correctly if you are only expecting one. Blake Coverett wrote: They are not the same and code for one will not handle the other correctly System.IO.Path does handle it.

                          B Offline
                          B Offline
                          Blake Coverett
                          wrote on last edited by
                          #14

                          using System;
                          class AClueForMark {
                          static void Main() {
                          Console.WriteLine(
                          Path.GetDirectoryName("http://foo.com/bar/baz.htm?x/y"));
                          }
                          }

                          Quick quiz - what does it print? -Blake

                          1 Reply Last reply
                          0
                          • M mittalpa

                            you broke my code Blake..:(( "http://samplesite.com/account/myaccount.aspx?abc=x/y" anywho.. if I use AbsolutePath instead of AbsoluteUri, I get "/account/myaccount.aspx". What about "http://samplesite.com" ? How do get that? Follow your goals, Means will follow you ---Gandhi---

                            B Offline
                            B Offline
                            Blake Coverett
                            wrote on last edited by
                            #15

                            Sorry, I forgot you wanted the site on the front too, better to use Request.Url.GetLeftPart(UriPartial.Path) instead of Request.Url.AbsolutePath. That gets you everything from the scheme through the path, not just the path. -Blake

                            M 1 Reply Last reply
                            0
                            • B Blake Coverett

                              Mark Nischalke wrote: Add a couple of slashes to the address used to get here. Still gets you here but may not parse correctly if you are only expecting one. Wrong. Try it and you will see for yourself. As I already told you, System.Uri class canonicalized the URL. If I use http://foo.com/bar///test.aspx, yes I will get the same page as http://foo.com/bar/test.aspx, but the value of Request.Url.AbsolutePath will be /bar/test.aspx with the extra slashes removed in both cases. Mark Nischalke wrote: System.IO.Path does handle it. System.IO.Path is for file system paths not URI paths. The two are not the same. Valid instances of one are not always valid instances of the other. In particular, System.IO.Path knows nothing about query strings or fragment identifiers. If you pass a valid Uri containing those into System.IO.Path you will end up with either an exception or the wrong result. Check your facts before you spout off next time. -Blake

                              N Offline
                              N Offline
                              Not Active
                              wrote on last edited by
                              #16

                              Blake Coverett wrote: In particular, System.IO.Path knows nothing about query strings or fragment identifiers. Given the criteria of finding the base folder for a page, then query strings on the url make no difference. Blake Coverett wrote: If you pass a valid Uri containing those into System.IO.Path you will end up with either an exception or the wrong result. string strURL = "http://mytest.com/subfolder/default.ass?id=clue"; string str = System.IO.Path.GetDirectoryName(strURL); Still waiting for the exception. Wait maybe this System.UriBuilder uri = new UriBuilder("http://mytest.com/subfolder/default.ass?id=blake"); string str = System.IO.Path.GetDirectoryName(uri.Uri.ToString()); Nope, still no exception.:confused:

                              B 1 Reply Last reply
                              0
                              • N Not Active

                                Blake Coverett wrote: In particular, System.IO.Path knows nothing about query strings or fragment identifiers. Given the criteria of finding the base folder for a page, then query strings on the url make no difference. Blake Coverett wrote: If you pass a valid Uri containing those into System.IO.Path you will end up with either an exception or the wrong result. string strURL = "http://mytest.com/subfolder/default.ass?id=clue"; string str = System.IO.Path.GetDirectoryName(strURL); Still waiting for the exception. Wait maybe this System.UriBuilder uri = new UriBuilder("http://mytest.com/subfolder/default.ass?id=blake"); string str = System.IO.Path.GetDirectoryName(uri.Uri.ToString()); Nope, still no exception.:confused:

                                B Offline
                                B Offline
                                Blake Coverett
                                wrote on last edited by
                                #17

                                Tsk, tsk, tsk. Are you actively trying to look more stupid here? Mark Nischalke wrote: Given the criteria of finding the base folder for a page, then query strings on the url make no difference. Incorrect, the Request.Url includes the query string and the fragment identifier. Any attempt to extract the base folder needs to take that into consideration. Mark Nischalke wrote: Still waiting for the exception. Read what I said, you will get either an exception or the wrong result. Either/or, that's one of those tricky English phrases, it means you will get one of them. 1) When the query string or fragment identifier contains a '/' or '\' you get the wrong result. 2) When the query string or fragment identifier contains a '|' or '>' or '<' etc an exception is thrown. All of these characters are perfectly legal in that part of a Uri.

                                using System;
                                using System.IO;
                                class MarkIsEvenMoreStupidThanIThought {
                                static void Main() {
                                Uri wrongResult = new Uri("http://foo.com/bar/baz.htm?x/y");
                                Uri exceptionThrown = new Uri("http://foo.com/bar/baz.htm?x>y");
                                Console.WriteLine(
                                Path.GetDirectoryName(wrongResult.ToString()));
                                Console.WriteLine(
                                Path.GetDirectoryName(exceptionThrown.ToString()));
                                }
                                }

                                So, are we completely clear now? System.IO.Path fails to properly parse perfectly legal URLs, giving either the wrong value or throwing an exception. I don't have a problem with people who don't know things, after all there's lots I don't know, but I have no tolerance for aggressively stupid people who won't learn when taught. -Blake

                                1 Reply Last reply
                                0
                                • B Blake Coverett

                                  Sorry, I forgot you wanted the site on the front too, better to use Request.Url.GetLeftPart(UriPartial.Path) instead of Request.Url.AbsolutePath. That gets you everything from the scheme through the path, not just the path. -Blake

                                  M Offline
                                  M Offline
                                  mittalpa
                                  wrote on last edited by
                                  #18

                                  here it is finally.. strFolder = Request.Url.GetLeftPart(UriPartial.Authority) & Left(Request.Url.AbsolutePath, InStrRev(Request.Url.AbsolutePath, "/")) :-D Follow your goals, Means will follow you ---Gandhi---

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups