Skip to main content
 首页 » 编程设计

c#之如何在 ashx webHandler ASP.NET 3.5 中读取 postdata

2024年08月12日48pengyingh

我正在尝试访问 ashx 网络处理程序中的发布数据,在我使用 Newtonsoft.Json 实现此目的的 MVC 项目中。

Dictionary<string, string> postData = JsonConvert.DeserializeObject<Dictionary<string, string>>(data); 

这导致了一个字典,我可以在其中访问帖子数据

  if (postData.ContainsKey("myKey")) 
    { 
        someVar = postData["myKey"]; 
    } 

在另一个 ASP.NET 3.5 项目中,我似乎没有可用的 Newtonsoft,我尝试使用:

object postData = new JavaScriptSerializer().DeserializeObject(data); 

这个方法应该返回一个对象图,它似乎工作正常,如果我在 visual studio 中使用调试器查看, 我可以看到我发布的数据:

如果我键入 postData,如何访问此对象中的数据?我得到的唯一选项是:Equals、GetHashCode、GetType 和 ToString

我试过 postData[0].value;

但这会报错:无法应用索引。

请您参考如下方法:

尝试使用自定义类型而不是对象,看看它是否有效

 public class yourKeyvalue 
    { 
        public string _Key{ get; set; } 
        public string _value{ get; set; } 
 
    } 
 
 
    var postData = new JavaScriptSerializer().Deserialize<yourKeyvalue>(data); 
    var Key = postData._Key  
    var Value = postData._value