C# 如何发出返回json的处理程序请求
我有一个返回json的处理程序C# 如何发出返回json的处理程序请求,c#,json,handler,C#,Json,Handler,我有一个返回json的处理程序 public void ProcessRequest (HttpContext context) { HttpResponse response = context.Response; response.ContentType = "application/json"; string controlType = context.Request.QueryString["controlType"]; JavaScriptSerial
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);
}
}
这里有一种从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);
}
}
考虑跨域限制,以便从不同的项目中使用它。如果您计划在不同的域中从javascript访问它,那么您需要使用jsonp。对op进行了更改。我需要知道如何从c端请求和使用,而不是JavaScripts。为了从不同的项目中使用它,请考虑跨域限制。如果您计划从不同域中的javascript访问此文件,则需要使用jsonp。对op进行了更改。我需要知道如何从c端请求和使用,而不是javascriptI,我需要能够解析返回的json。有没有可能有一个var,然后我就可以循环使用json?你可以使用jsonSerializer.Deserialize,但你需要创建一个类来映射json结构。我将尝试使用javaScriptSerializer.Deserializejson,看看这是否有效。不过,你的例子很可靠。非常感谢。我需要能够解析返回的json。有没有可能有一个var,然后我就可以循环使用json?你可以使用jsonSerializer.Deserialize,但你需要创建一个类来映射json结构。我将尝试使用javaScriptSerializer.Deserializejson,看看这是否有效。不过,你的例子很可靠。非常感谢。