1

I have a ASP.NET MVC controller which is making call to another service using HttpClient class.

             var url = "some url";
             var client = new HttpClient();
             var result= client.GetAsync(url);

The URL I am sending contains some special characters. How can encode special characters in ASP.NET MVC controller?

Thanks!!1

4 Answers 4

4

Try this:

url = HttpUtility.UrlEncode(url);
Sign up to request clarification or add additional context in comments.

Comments

0

As you are considering a URL that will be used as such for a request (with HttpClient.getAsync) -- not as an argument within another URL -- you should use Uri.EscapeUriString.

Here is a comparison of three methods for the following URL:

var url = "http://some url?data=x y+z&user=1#ok";

HttpUtility.UrlEncode

Console.WriteLine(HttpUtility.UrlEncode(url));

http%3a%2f%2fsome+url%3fdata%3dx+y%2bz%26user%3d1%23ok

Obviously, this is not desired: the URL got damaged with / escaped, a + entered in the path, ...etc. The method seems useful for the query part of the URL, but not for the whole lot.

HttpUtility.UrlPathEncode

Console.WriteLine(HttpUtility.UrlPathEncode(url));

http://some%20url?data=x y+z&user=1#ok

This looks useful, although the space is a bit of a problem in the query part (notice the broken hyperlinking here, although browser can deal with it). But more importantly, the method is being deprecated:

Do not use; intended only for browser compatibility. Use UrlEncode.

Uri.EscapeUriString

Console.WriteLine(Uri.EscapeUriString(url));

http://some%20url?data=x%20y+z&user=1#ok

This seems to do the job well: %20 is an escape sequence that all modern browsers should support, also when occurring in the query part of the URL.

Comments

-1

There is no need it encoding in Razor View Engine starting from 3rd version, and it's very convenient. Instead if you want to use tags you should use:

@Html.Raw(myString)

So basically just using Razor comes with encoding by default.

2 Comments

Thank you for reply. But I am not using any view. Instead from my controller I am making call to another service.
@Brown_Dynamite Ok, you asked for utility in asp.net mvc, that's why I've decided you are using views. Then use HttpUtility.HtmlEncode instead of UrlEncode, because it suits server.htmlencode better. Just add proper reference to project.
-1

You should use HttpUtility.UrlPathEncode

When you use url = HttpUtility.UrlEncode(url) it doesn't work fine with spaces.

2 Comments

This answer goes against the instruction on the documentation page: "Do not use; intended only for browser compatibility. Use UrlEncode.". Please consider removing your answer so people do not get misinformed.
Did you have a chance to test it? When i used UrlEncode i had a problems with spaces. For example: "Test Body" was encoded as Test+Body. But UrlPathEncoed works as expected. Please try to test it @trincot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.