C# .Net How to Encode URL space with %20 instead of + [duplicate] - c#

This question already has answers here:
URL Encoding using C#
(14 answers)
Closed 5 years ago.
How to encode query string space with %20 instead of + ?
Because System.Web HttpUtility.UrlEncode() gives the space with +.

https://msdn.microsoft.com/en-us/library/system.uri.escapeuristring(v=vs.110).aspx
var encoded = Uri.EscapeUriString("How to encode");
OUTPUT:
How%20to%20encode

Related

C# Regex replace mutliple backslash(`\`) with single [duplicate]

This question already has answers here:
Why is there an second '\' when I get my path
(2 answers)
Why does .NET add an additional slash to the already existent slashes in a path?
(4 answers)
File name has two backslashes C#
(5 answers)
Closed 3 months ago.
I want to replace all occurences of multiple slash ('') with single backslash
For example
mama\\\\Please\\\\\U0U0001f973\U00.txt
Result should be this
mama\Please\U0U0001f973\U00.txt
I have tried
string file = "mama\\\\Please\\\\\U0U0001f973\U00.txt"
string output = Regex.Replace(file, #"[/\\]{2,}", #"\");
current result
mama\\Please\\\U0U0001f973\U00.txt

C# string replace for website names [duplicate]

This question already has answers here:
How do I write a backslash (\) in a string?
(6 answers)
Closed 7 years ago.
I am trying to do a string replace for website names. Here is the code:
string output = input.Replace("C:\Design\Website\", "Someting");
TextBox.Text = osc.output;
The code is incorrect as there is an issue \ with these marks. How can I fix my code?
You need to escape the \:
Either:
"C:\\Design\\Website\\"
Or:
#"C:\Design\Website\"

What does "#" mean before a string? [duplicate]

This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 8 years ago.
What does "#" mean before a string?
I saw this notation with paths:
string myfolder = #"C:\Users\";
But also with normal strings.
It means it's a literal string, so won't treat \ as an escape character, for example. This page should help you understand it better.

Equivalent to Javascript's encodeURI? [duplicate]

This question already has answers here:
Does C# have an equivalent to JavaScript's encodeURIComponent()?
(7 answers)
UrlEncode - Javascript vs. C# [duplicate]
(5 answers)
Closed 9 years ago.
C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So
"http://www.example.com/my cool page"
would be encoded to
"http://www.example.com/my%20cool%20page"
Is there something baked into .NET to do this? Or is a regex my best bet?
Try
Uri.EscapeUriString("http://www.mysite.com/my cool page")
Try
Server.URLEncode(uri.ToString)
Try this:
HttpUtility.UrlEncode(String)
For example:
var url_encoded_string = HttpUtility.UrlEncode(userInput);
You could use System.Net.WebUtility.UrlEncode(string value).

include value with '&' in querystring [duplicate]

This question already has answers here:
How to build a query string for a URL in C#?
(39 answers)
Closed 9 years ago.
I have to write this data in querystring:
http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you
I got an error because of that symbol '&' i used.
In c# you can use this:-
HttpUtility.UrlEncode("http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you");
HttpUtility is a part of System.Web and this will ensure an of the non permitted query string char are url Encoded.
Once you do this you will get something like this http%3a%2f%2flocalhost%3a1256%2f4.market.ph.local%2fWEP%2fAdd.cshtml%3fdata%3dme%2b%26%2byou
On the receiver just decode it back.
Use urlencode($yourstring) or if you are hard coding it, use %26 to represent the ampersand.

Categories

Resources