Why does Uri partially decode the query part?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















Background: I'm trying to some some URL manipulation for an MVC application. I've written an HtmlHelper extension method that replaces on query string value with another. The method takes the current request string, manually splits the query part (so as to prevent Foo=1&Foo=Hello%2c%20World!&Foo=2 from being read as Foo=1,Hello, World,2), and then tries to use UriBuilder and Uri to get a URL.



Problem: For some reason, Uri.ToString partially decodes query strings. As a quick test, I fired up LINQPad and ran the following:



var ub = new System.UriBuilder("http://foobar");
ub.Query = "NY%26A"; // NY&A
ub.Uri.Dump();
ub.Uri.ToString().Dump();
ub.Uri.Query.Dump();
ub.ToString().Dump();


The results are:



http://foobar/?NY&A
http://foobar/?NY&A
?NY%26A
http://foobar:80/?NY%26A


I say partially decoding, because if I use lots of escaped characters, it decodes some and leaves others in place:



var ub = new System.UriBuilder("http://foobar");
ub.Query = "!%40%23%24%25%5E%26*()"; // !@#$%^&*()
ub.Uri.Dump();
ub.Uri.ToString().Dump();
ub.Uri.Query.Dump();
ub.ToString().Dump();


And the results:



http://foobar/?!@%23$%25^&*()
http://foobar/?!@%23$%25^&*()
?!%40%23%24%25%5E%26*()
http://foobar:80/?!%40%23%24%25%5E%26*()


Question: Can someone explain what is going on?










share|improve this question





























    2















    Background: I'm trying to some some URL manipulation for an MVC application. I've written an HtmlHelper extension method that replaces on query string value with another. The method takes the current request string, manually splits the query part (so as to prevent Foo=1&Foo=Hello%2c%20World!&Foo=2 from being read as Foo=1,Hello, World,2), and then tries to use UriBuilder and Uri to get a URL.



    Problem: For some reason, Uri.ToString partially decodes query strings. As a quick test, I fired up LINQPad and ran the following:



    var ub = new System.UriBuilder("http://foobar");
    ub.Query = "NY%26A"; // NY&A
    ub.Uri.Dump();
    ub.Uri.ToString().Dump();
    ub.Uri.Query.Dump();
    ub.ToString().Dump();


    The results are:



    http://foobar/?NY&A
    http://foobar/?NY&A
    ?NY%26A
    http://foobar:80/?NY%26A


    I say partially decoding, because if I use lots of escaped characters, it decodes some and leaves others in place:



    var ub = new System.UriBuilder("http://foobar");
    ub.Query = "!%40%23%24%25%5E%26*()"; // !@#$%^&*()
    ub.Uri.Dump();
    ub.Uri.ToString().Dump();
    ub.Uri.Query.Dump();
    ub.ToString().Dump();


    And the results:



    http://foobar/?!@%23$%25^&*()
    http://foobar/?!@%23$%25^&*()
    ?!%40%23%24%25%5E%26*()
    http://foobar:80/?!%40%23%24%25%5E%26*()


    Question: Can someone explain what is going on?










    share|improve this question

























      2












      2








      2








      Background: I'm trying to some some URL manipulation for an MVC application. I've written an HtmlHelper extension method that replaces on query string value with another. The method takes the current request string, manually splits the query part (so as to prevent Foo=1&Foo=Hello%2c%20World!&Foo=2 from being read as Foo=1,Hello, World,2), and then tries to use UriBuilder and Uri to get a URL.



      Problem: For some reason, Uri.ToString partially decodes query strings. As a quick test, I fired up LINQPad and ran the following:



      var ub = new System.UriBuilder("http://foobar");
      ub.Query = "NY%26A"; // NY&A
      ub.Uri.Dump();
      ub.Uri.ToString().Dump();
      ub.Uri.Query.Dump();
      ub.ToString().Dump();


      The results are:



      http://foobar/?NY&A
      http://foobar/?NY&A
      ?NY%26A
      http://foobar:80/?NY%26A


      I say partially decoding, because if I use lots of escaped characters, it decodes some and leaves others in place:



      var ub = new System.UriBuilder("http://foobar");
      ub.Query = "!%40%23%24%25%5E%26*()"; // !@#$%^&*()
      ub.Uri.Dump();
      ub.Uri.ToString().Dump();
      ub.Uri.Query.Dump();
      ub.ToString().Dump();


      And the results:



      http://foobar/?!@%23$%25^&*()
      http://foobar/?!@%23$%25^&*()
      ?!%40%23%24%25%5E%26*()
      http://foobar:80/?!%40%23%24%25%5E%26*()


      Question: Can someone explain what is going on?










      share|improve this question














      Background: I'm trying to some some URL manipulation for an MVC application. I've written an HtmlHelper extension method that replaces on query string value with another. The method takes the current request string, manually splits the query part (so as to prevent Foo=1&Foo=Hello%2c%20World!&Foo=2 from being read as Foo=1,Hello, World,2), and then tries to use UriBuilder and Uri to get a URL.



      Problem: For some reason, Uri.ToString partially decodes query strings. As a quick test, I fired up LINQPad and ran the following:



      var ub = new System.UriBuilder("http://foobar");
      ub.Query = "NY%26A"; // NY&A
      ub.Uri.Dump();
      ub.Uri.ToString().Dump();
      ub.Uri.Query.Dump();
      ub.ToString().Dump();


      The results are:



      http://foobar/?NY&A
      http://foobar/?NY&A
      ?NY%26A
      http://foobar:80/?NY%26A


      I say partially decoding, because if I use lots of escaped characters, it decodes some and leaves others in place:



      var ub = new System.UriBuilder("http://foobar");
      ub.Query = "!%40%23%24%25%5E%26*()"; // !@#$%^&*()
      ub.Uri.Dump();
      ub.Uri.ToString().Dump();
      ub.Uri.Query.Dump();
      ub.ToString().Dump();


      And the results:



      http://foobar/?!@%23$%25^&*()
      http://foobar/?!@%23$%25^&*()
      ?!%40%23%24%25%5E%26*()
      http://foobar:80/?!%40%23%24%25%5E%26*()


      Question: Can someone explain what is going on?







      .net uri






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 25 '12 at 17:49









      Don 01001100Don 01001100

      1,0031338




      1,0031338
























          3 Answers
          3






          active

          oldest

          votes


















          1














          It appears to be using "safe" decoding. In your second example, it won't decode # or %, because that would change the structure/meaning of the URI.



          If you don't want that decoding to happen, stop calling .ToString() on the URI, and instead access the components you want directly.






          share|improve this answer
























          • Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

            – Don 01001100
            Apr 25 '12 at 20:07



















          1














          Because URI does use url encode



          you can use this to read the original or add encoded value to URI object:



                  System.Web.HttpUtility.UrlDecode("NY%26A");
          System.Web.HttpUtility.UrlEncode("NY&A");





          share|improve this answer































            0














            If the Uri was constructed from a string, Uri.OriginalString is an easy way to get back to that original string, with the original encoding.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10321003%2fwhy-does-uri-partially-decode-the-query-part%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              It appears to be using "safe" decoding. In your second example, it won't decode # or %, because that would change the structure/meaning of the URI.



              If you don't want that decoding to happen, stop calling .ToString() on the URI, and instead access the components you want directly.






              share|improve this answer
























              • Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

                – Don 01001100
                Apr 25 '12 at 20:07
















              1














              It appears to be using "safe" decoding. In your second example, it won't decode # or %, because that would change the structure/meaning of the URI.



              If you don't want that decoding to happen, stop calling .ToString() on the URI, and instead access the components you want directly.






              share|improve this answer
























              • Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

                – Don 01001100
                Apr 25 '12 at 20:07














              1












              1








              1







              It appears to be using "safe" decoding. In your second example, it won't decode # or %, because that would change the structure/meaning of the URI.



              If you don't want that decoding to happen, stop calling .ToString() on the URI, and instead access the components you want directly.






              share|improve this answer













              It appears to be using "safe" decoding. In your second example, it won't decode # or %, because that would change the structure/meaning of the URI.



              If you don't want that decoding to happen, stop calling .ToString() on the URI, and instead access the components you want directly.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 25 '12 at 17:54









              dlevdlev

              42.3k5106125




              42.3k5106125













              • Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

                – Don 01001100
                Apr 25 '12 at 20:07



















              • Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

                – Don 01001100
                Apr 25 '12 at 20:07

















              Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

              – Don 01001100
              Apr 25 '12 at 20:07





              Thanks for the reply. I wonder why it's decoding &, since that also changes the meaning of the URI. My goal was to be able to take an existing URI, change something, and get whole URI back out. It was easy enough to do without the .NET classes, but I wanted to understand what was going on.

              – Don 01001100
              Apr 25 '12 at 20:07













              1














              Because URI does use url encode



              you can use this to read the original or add encoded value to URI object:



                      System.Web.HttpUtility.UrlDecode("NY%26A");
              System.Web.HttpUtility.UrlEncode("NY&A");





              share|improve this answer




























                1














                Because URI does use url encode



                you can use this to read the original or add encoded value to URI object:



                        System.Web.HttpUtility.UrlDecode("NY%26A");
                System.Web.HttpUtility.UrlEncode("NY&A");





                share|improve this answer


























                  1












                  1








                  1







                  Because URI does use url encode



                  you can use this to read the original or add encoded value to URI object:



                          System.Web.HttpUtility.UrlDecode("NY%26A");
                  System.Web.HttpUtility.UrlEncode("NY&A");





                  share|improve this answer













                  Because URI does use url encode



                  you can use this to read the original or add encoded value to URI object:



                          System.Web.HttpUtility.UrlDecode("NY%26A");
                  System.Web.HttpUtility.UrlEncode("NY&A");






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 25 '12 at 17:59









                  SweetSweet

                  1561413




                  1561413























                      0














                      If the Uri was constructed from a string, Uri.OriginalString is an easy way to get back to that original string, with the original encoding.






                      share|improve this answer




























                        0














                        If the Uri was constructed from a string, Uri.OriginalString is an easy way to get back to that original string, with the original encoding.






                        share|improve this answer


























                          0












                          0








                          0







                          If the Uri was constructed from a string, Uri.OriginalString is an easy way to get back to that original string, with the original encoding.






                          share|improve this answer













                          If the Uri was constructed from a string, Uri.OriginalString is an easy way to get back to that original string, with the original encoding.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 22:24









                          Jared PhelpsJared Phelps

                          162210




                          162210






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10321003%2fwhy-does-uri-partially-decode-the-query-part%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Wiesbaden

                              Marschland

                              Dieringhausen