Posted By

housecor on 01/19/11


Tagged

post multiple comma delimited instances


Versions (?)

Pull posted data directly from StreamReader when traditional comma delimited list won't suffice


 / Published in: C#
 

Useful when creating multiple instances of field with same name on client-side, but since the user can enter a comma, referencing Request["myvar"] will get you a comma delimited list that isn't reliable (the commas aren't escaped). So use StreamReader to read the raw http stream and parse it yourself.

  1. /// <summary>
  2. /// Save the FAQs posted from traditional html inputs created via jquery.
  3. /// Since the form inputs all have the same name, they'd be comma delimited in Request.Form
  4. /// which is a problem if someone types a comma, so just parsing the raw http post to get
  5. /// what we need via Request.InputStream. Would be nice if .Net offered something like Request("ids").item(i)
  6. /// but no such luck.
  7. /// </summary>
  8. private void GetFAQsFromPost()
  9. {
  10. using (StreamReader sr = new StreamReader(Request.InputStream))
  11. {
  12. string[] postedValues = sr.ReadLine().Split('&');
  13. foreach (string postedValue in postedValues)
  14. {
  15. if (postedValue.StartsWith("question="))
  16. {
  17. string urlEncodedQuestion = postedValue.Replace("question=", "");
  18. Questions.Add(HttpUtility.UrlDecode(urlEncodedQuestion));
  19. }
  20. else if (postedValue.StartsWith("answer="))
  21. {
  22. string urlEncodedAnswer = postedValue.Replace("answer=", "");
  23. Answers.Add(HttpUtility.UrlDecode(urlEncodedAnswer));
  24. }
  25. }
  26. }
  27. }

Report this snippet  

You need to login to post a comment.