0

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()));
ozmert75
  • 123
  • 1
  • 15

5 Answers5

4

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-

Community
  • 1
  • 1
Yogi
  • 9,174
  • 2
  • 46
  • 61
1

Your problem exactlly in the {"1"} part. The double quotation mark " should be outside the {}, not inside them. here is the fixed code.

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

or

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

and if you are using C# 6 then you can write it as following

string path = $"{Uri.EscapeDataString(proposalId.ToString())},ProposalName:\"{Uri.EscapeDataString(proposalName)}\",VendorId:{Uri.EscapeDataString(vendorId.ToString())}";
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
1

This might do the trick for you

\"{1}\"

instead of

{"1"}

because you can put \ symbol to indicate escape sequence followed by a reserved characters

So

string.Format("{{{0},ProposalName:\"{1}\",VendorId:{2}}}",
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

I think escaping the quotes and placing them outside the brackets will work:

"{{0},ProposalName:\"{1}\",VendorId:{2}}"

Depending on the C# version, you can also do it like this, which I often think is an easier and cleaner way to do it:

 string path = $"{proposalId},ProposalName:\"{proposalName}\",VendorId:{vendorId}";
0

You have two problems:

  • Wrong quotation (should be outside the braces {...} and escaped)
  • Incorrect { and } escape: {{ means just a single '{' in a formatting string

Should be

string path = string.Format("{{{0},ProposalName:\"{1}\",VendorId:{2}}}",

please, notice

  • escaped quotations \" which are outside {1}
  • tripled curly braces{{{ and }}}

Edit: in your edited question you have the same errors:

string format = 
  "http://mobile.teklifdosyam.com/VendorReport/GetListProposalService?&page=1&start=0&limit=10&filter=" + 
   "{{ProposalId:{0},ProposalName:\"{1}\",VendorId:{2}}}";

string path = string.Format(format,
  Uri.EscapeDataString(proposalId.ToString()),
  Uri.EscapeDataString(proposalName),
  Uri.EscapeDataString(vendorId.ToString()));

please, notice escaped quotations \" which are outside the {1}, double '{{' and tripled '}}}'. When formatting you have to use numbers as place holders: so {"proposalName"} must be changed into {0}

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215