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()));

5 Answers 5

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-

Sign up to request clarification or add additional context in comments.

1 Comment

@ozmert75 - Edited my answer for your updated question.
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())}";

Comments

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}}}",

Comments

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}";

Comments

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}

Comments

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.