ozmert75 ozmert75 - 11 days ago 5
C# Question

C# string format error

I need to send the value I receive from the model with this link, the proposalName field must be in quotes.How can I do it?

Here is my service url.

string path = string.Format("{ProposalId:{proposalId},ProposalName:{"proposalName"},VendorId:{vendorId}}",
Uri.EscapeDataString(proposalId.ToString()),
Uri.EscapeDataString(proposalName),
Uri.EscapeDataString(vendorId.ToString()));

Answer

You can simply put quotes around by escaping the quotes, like this -

string path = string.Format("{{0},ProposalName:\"{1}\",VendorId:{2}}",
                   Uri.EscapeDataString(proposalId.ToString()),
                   Uri.EscapeDataString(proposalName),
                   Uri.EscapeDataString(vendorId.ToString()));

As per your updated question, if you need to pass double quotes in URL, you need to encode it to %22

You can also use URI which allows a lot of flexibility with urls. For example -

Uri myUri = new Uri("http://google.com/search?hl=en&q=\"query with quotes\"");

Going with your example - Replace EscapeDataString with Uri.EscapeUriString. It will escape the chracter to form a valid URL. " will get replaced by %22

Some suggestions here and here-

Comments