Reputation: 53
According to RFC 3986 the valid characters for the path component are:
a-z A-Z 0-9 . - _ ~ ! $ & ' ( ) * + , ; = : @
as well as percent-encoded characters and of course, the slash /
.
I can however not find a class that converts a string to well formatted path according to above rules.
string rawPath = "/A9_(+@*)/# ?/";
string expectedPath = "/A9_(+@*)/%23%20%3f";
However, see code below:
string rawPath = "/A9_(+@*)/# ?/";
Uri.EscapeDataString(rawPath); //Output=>%2FA9_%28%2B%40%2A%29%2F%23%20%3F%2F
Uri.EscapeUriString(rawPath); //Ouput=>/A9_(+@*)/#%20?/
HttpUtility.UrlPathEncode(rawPath); //Ouput=>/A9_(+@*)/#%20?/
Nothing I tried leaves allowed characters unescaped and propery escapes all other characters.
Hopefully somebody can save me from having to write my own utility!
Upvotes: 4
Views: 3724
Reputation: 66
Each of these provides an expected encoding based on various rules within the creation of the URI. This is because of the inclusion of Reserved Characters (See section 2.2 of RFC 3968), which include ?
and #
which are delimiters defined by the standard. These would not be escaped because they are part of the language definition.
Upvotes: 1