How to perform urlencode

Looking for a method to encode a URL but cannot find one. Can some point me in the right direction (NETMF 4.3).
Thanks

I think that you will have to write your own (or find an implementation on the web). I have seen questions of this nature in the past and the developer has always provided their own implementation of this method.

Regards,
Mark

Thanks Mark, that is what I ended up doing.

How do I insert a code snippet into the blog? Will supply the code I wrote in case its of help to anyone else.

Thanks
Joe

Thank you being willing to share your work.

This site allows the use of markdown to format messages. Git has a Markdown guide which covers the basics. Scroll down the page and look for the Syntax Highlighting section. The language tag for C# is the three back ticks followed by the word csharp. Note that this is in lower case. Code should look like the following when formatted correctly.

int i = 0;

Regards,
Mark

Thanks again Mark

In case anyone is interested here is the routine I wrote for the URL encoding. It does not cater for all the possible characters that need escaping but handled the ones I needed.

Note: Space can be replaced by either %20 or the + sign depending on whether its Url or Uri encoding. I have opted for + sign as I need to match what the TextEncoder.Encode() does in .Net Core 2.0.

        /// <summary>
        /// Simple URL encoder that replaces characters from URL and replaces with percentage escaped chars
        /// </summary>
        /// <param name="_input">Url to encode</param>
        /// <returns>Encoded Url</returns>
        public static string SimpleUrlEncode(string _input)
        {
            StringBuilder _encoded = new StringBuilder();
            foreach( char _chr in _input)
            {
                switch(_chr)
                {
                    case ' ':
                        //_encoded.Append("%20");
                        _encoded.Append("+"); // Trying to match the output of the .Net Core TextEncoder.Encode()
                        break;
                    case '!':
                        _encoded.Append("%21");
                        break;
                    case '"':
                        _encoded.Append("%22");
                        break;
                    case '#':
                        _encoded.Append("%23");
                        break;
                    case '$':
                        _encoded.Append("%24");
                        break;
                    case '%':
                        _encoded.Append("%25");
                        break;
                    case '&':
                        _encoded.Append("%26");
                        break;
                    case '-':
                        _encoded.Append("%2D");
                        break;
                    case '/':
                        _encoded.Append("%2F");
                        break;
                    case ':':
                        _encoded.Append("%3A");
                        break;
                    default:
                        _encoded.Append(_chr);
                        break;
                }
            }
            return _encoded.ToString();
        }
1 Like

Thank you for sharing,
Mark