1

我有一个返回 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 对象。

  1. HttpRequest将是用于向处理程序发出请求的适当对象吗?
  2. 有人可以提供一个简单的例子来说明如何从另一个 c# 类(不是 javascript)请求和使用 json 对象吗?
4

1 回答 1

1

在这里,您可以使用 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);
    }
}
于 2012-05-17T18:45:57.123 回答