代码获取特定虚拟路径编译页(ashx)的一个实例并获取数据
//获得数据 - jsonResponse
string jsonResponse;
JObject jRequest = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.Params["request"])));
string url = (string)jRequest["url"];
url = "~/" + url;
JToken jToken;
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter writer = new HtmlTextWriter(sw);
HttpResponse response = new HttpResponse(writer);
List<string> queryParams = new List<string>();
queryParams.Add("DateFormat=text");
foreach (KeyValuePair<string, JToken> property in (jRequest["params"] as JObject))
queryParams.Add(property.Key + "=" + HttpUtility.UrlEncode((string)property.Value, Encoding.Default));
HttpRequest callrequest = new HttpRequest(null, context.Request.Url.ToString(), String.Join("&", queryParams.ToArray()));
HttpContext callcontext = new HttpContext(callrequest, response);
IHttpHandler handler = PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
handler.ProcessRequest(callcontext);
jsonResponse = sw.ToString();
jToken = JToken.Parse(jsonResponse);
if (jToken is JObject)
{
JObject jObject = jToken as JObject;
if (jObject["success"] != null && jObject["success"].Type == JTokenType.Boolean && (bool)jObject["success"] == false)
throw new Exception((string)jObject["errorMessage"]);
}
}