I have an MVC application that returns ResultObjekt
after processing a FormulaData
object. This is an API for relaxing through HTTP Post
[HttpPost] [ActionName("GetResult")] public ResultObjekt GetResult([FromBody]FormularData values) { }
Question: Is there a way to read all the properties from values
in a Dictionary<string, string>
or IEnumerable<KeyValuePair<string, string>>
?
eg.
public class FormularData { public string Item1 { get; set; } public string Item2 { get; set; } }
should result in Dictionary<string,string>()
or IEnumerable<KeyValuePair<string, string>>
with the values { {"Item1","Value1"}, {"Item2","Value2"}}
My previous solution worked with Querystring
and HttpGet
instead of HttpPost
, and since I changed, Request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value)
no longer works.
Here is my current - not so pretty solution:
[HttpPost] [ActionName("GetResult")] public ResultObjekt GetResult([FromBody]FormularData values) { List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); if (!string.IsNullOrEmpty(values.Item1)) { list.Add(new KeyValuePair<string, string>("Item1", values.Item1)); } if (!string.IsNullOrEmpty(values.Item2)) { list.Add(new KeyValuePair<string, string>("Item2", values.Item2)); } IEnumerable<KeyValuePair<string, string>> result = list.AsEnumerable(); }
Toshi source share