如何发出返回 json 的处理程序请求
本文关键字:处理 程序 请求 json 何发出 返回 | 更新日期: 2023-09-27 18:34:26
我有一个返回 json 的处理程序
public void ProcessRequest (HttpContext context) {
HttpResponse response = context.Response;
response.ContentType = "application/json";
string controlType = context.Request.QueryString["controlType"];
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
CmsManager cmsManager = new CmsManager();
IDictionary<string, Guid> sitefinityPageDictionary = SitefinityUtilities.sitefinityPageDictionary;
if (string.IsNullOrEmpty(controlType)) {
response.Write("0");
}
else {
var pagesWithControl = from page in sitefinityPageDictionary
from control in cmsManager.GetPage(page.Value).Controls
where control.TypeName == controlType
select page;
response.Write(jsonSerializer.Serialize(pagesWithControl));
}
}
在一个单独的项目中,我想向处理程序发出请求以使用返回的 json 对象。
HttpRequest
是否是用于向处理程序发出请求的合适对象?- 有人可以提供一个简单的示例来说明如何从另一个 c# 类(不是 javascript)请求和使用 json 对象吗?
这里有一种方法可以使用 c# 中的 json 对象
IList<MyClass> myClassList = null;
var request = WebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8";
string json;
using (var response = request.GetResponse())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
json = sr.ReadToEnd();
var javaScriptSerializer = new JavaScriptSerializer();
myClassList = javaScriptSerializer.Deserialize<List<MyClass>>(json);
}
}