将FormCollection转换为JSON

Sle*_*lee 2 asp.net-mvc asp.net-ajax

我有:

Function SaveAnswers(ByVal collection As FormCollection) As ActionResult

End Funciton
Run Code Online (Sandbox Code Playgroud)

我想将集合转换为JSON,我认为有一个序列化器可以做到这一点,但似乎无法找到它?

ptu*_*utt 11

序列化FormCollection对象对我来说不起作用,键序列化,但值没有.

我想用一种简单的方法来"记录"FormCollection值,以便在测试用例中重用.为此,我创建了一个扩展方法:

public static string ToJSON(this System.Web.Mvc.FormCollection collection)
{
    var list = new Dictionary<string, string>();
    foreach (string key in collection.Keys)
    {                
        list.Add(key, collection[key]);
    }
    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
}
Run Code Online (Sandbox Code Playgroud)