2019年8月

.Net C#的web服务

添加Api.ashx文件
ProcessRequest中只留WebApi.Exec();
添加App_Code文件夹
建立WebApi类,引用Newtonsoft.Json.dll
添加Exec方法
HttpContext context = HttpContext.Current;
获取Post的信息
Stream stream = context.Request.InputStream;
StreamReader streamReader = new StreamReader(stream);
string post = streamReader.ReadToEnd();
转换为json
JObject ji = (JObject)JsonConvert.DeserializeObject(post);
获取Get的信息
string x = context.Request["x"]..ToString();
构造输出json
JObject jo = new JObject();
jo.Add("msg", "hello");
添加执行时间,开始部分
DateTime dt = DateTime.Now;
结束前
TimeSpan ts = DateTime.Now - dt;
jo.Add("cost", ts.TotalSeconds.ToString() + "s");
输出信息
string json = jo.ToString(Formatting.Indented);
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(json);
Api.ashx设为起始页运行之