Search code examples
c#request.form

c# Request.Form to string and back to Request.Form


I'm facing this problem. I pass the request form as a string to a webservice, and then inside it I'm trying to fetch the data inside that string. I guess I could work it out searching for each substring but I wonder if there's a way of creating back a collection the way the Request.Form is.

Thanks!!

You're right davenewza, sorry for that

The string will look basically like this

title_1=Mr&initials_1=James&surname_1=Smith&title_2=Mr&initials_2=Harry&surname_2=Smith&Address=TestAddress&City=Surrey&

although it will be too long to paste it entirely it will be this way, just the result of pass ToString a RequestForm, and what I would like to get is the actual value of the initials_1, surname_1, initials_2, Address..... so the easiest way would be if I could transform it back to a Dictionary, isn't it? but don't know if it's possible


Solution

  • Something like this should do it

    string request = ...
    Dictionary<string,string> param = new Dictionary<string,string>();
    request.Split('&')
           .Select(o => o.Split('='))
           .ToList()
           .ForEach(p => param.Add(p[0],p[1])
    );
    

    But you have to be sure only parameter appears only once. Otherwise, you could do p => param[p[0]] = p[1], then you would replace previous versions... If you want to keep them all, you'll need a Dictionary>