JsonResult
返回类型了,方法名,对于访问web api不受影响, 而其中不同方法的参数个数不能有重的,否则不执行. ajax 中可以写两个链接,但最终只执行最上面
的,也不完全对,它会执行第一个,不受全称的限制
CORS
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using WEBapi.Models;
namespace WEBapi.Controllers
{
///
/// 微信控制器
///
public class WechatController : ApiController
{
private const string TOKEN = "bill";
///
///
///
///
///
[HttpPost]
[HttpGet]
public ApiResult Service()
{
// JObject jObject
//dynamic dic = jObject;
//string name = dic.name;
//string age = dic.age;
//int i = 123;
//int j = i;
//j = 456;
//Debug.WriteLine("i=" + i + ", j=" + j);
//ApiResult a = new ApiResult { IsSucceed = true, Message = "张三" };
//ApiResult b = a;
//b.IsSucceed = false;
//b.Message = "李四";
//Debug.WriteLine("a=" + a.Message + ", b=" + b.Message);
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传
统context
HttpRequestBase request = context.Request;//定义传统request对象
HttpResponseBase response = context.Response;
if (request.RequestType.Equals("POST"))
{
TextReader reader = new StreamReader(request.InputStream);
string content = reader.ReadToEnd();
Debug.WriteLine(content);
}
string signature = request.QueryString["signature"].ToString();
string timestamp = request.QueryString["timestamp"].ToString();
string nonce = request.QueryString["nonce"].ToString();
string echoStr = request.QueryString["echoStr"].ToString();
if (string.IsNullOrEmpty(echoStr))
{
response.End();
}
if (CheckSignature(signature, timestamp, nonce, TOKEN))
{
if (!string.IsNullOrEmpty(echoStr))
{
response.Write(echoStr);
// response.Output.Write("hello");
response.End();
}
}
return new ApiResult { IsSucceed = false, ErrorCode = -1 };
//HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取
传统context
//HttpRequestBase request = context.Request;//定义传统request对象
//HttpResponseBase response = context.Response;
//if (string.IsNullOrEmpty(request.QueryString["echoStr"]))
//{
// response.End();
//}
//string echoStr = request.QueryString["echoStr"].ToString();
//if (checkSignature())
//{
// if (!string.IsNullOrEmpty(echoStr))
// {
// response.Write(echoStr);
// // response.Output.Write("hello");
// response.End();
// }
//}
//if(h())
//{
// // return new JsonResult();
// return "hello";
//}
//return "hello";
// Debug.WriteLine(Request.Content);
}
private bool CheckSignature(string signature, string timestamp, string nonce, string token)
{
string[] arrTmp = { token, timestamp, nonce };
Array.Sort(arrTmp); //字典排序
string tmpStr = string.Join("", arrTmp); //使用指定的字符分隔
// 加密
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
return string.Equals(tmpStr, signature);
}
//
// GET: /DynamicTest/
//[System.Web.Http.HttpPost]
//public string Test(dynamic X)
//{
// return X.name + "hell";
//}
//[System.Web.Http.HttpPost]
//public JsonResult Entry(dynamic X)
//{
// return new JsonResult();
//}
//[System.Web.Http.HttpGet]
////public string Test2(Class1 n) //错误,不能用类做参数
////{
//// // Class1 str = Newtonsoft.Json.JsonConvert.DeserializeObject
(strQuery);
//// return "hello"+n.name;
////}
////[HttpGet]
//public string Test2(string name, int age)
//{
// return "hello" + age;
//}
//public bool checkSignature()
//{
// HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取
传统context
// HttpRequestBase request = context.Request;//定义传统request对象
// string signature = request.QueryString["signature"].ToString();
// string timestamp = request.QueryString["timestamp"].ToString();
// string nonce = request.QueryString["nonce"].ToString();
// string[] ArrTmp = { "3", timestamp, nonce };
// Array.Sort(ArrTmp); //字典排序
// string tmpStr = string.Join("", ArrTmp); //使用指定的字符分隔
// //加密
// tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
// tmpStr = tmpStr.ToLower();
// if (tmpStr == signature)
// {
// return true;
// }
// else
// {
// return false;
// }
//}
//[HttpPost]
//[HttpGet]
//public bool h(JObject jObject)
//{
// dynamic dic = jObject;
// string name = dic.name;
// string age = dic.age;
// return true;
//}
/*
public class WechatController : ApiController
{
//
// GET: /Wechat/
//define("TOKEN", "weixin");
// public string Token = "bill";
[System.Web.Http.HttpPost]
// [System.Web.Http.HttpGet]
public JsonResult Entry(dynamic X)
{
// Debug.WriteLine(Request.Content);
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获
取传统context
HttpRequestBase request = context.Request;//定义传统request对象
HttpResponseBase response = context.Response;
if (string.IsNullOrEmpty(request.QueryString["echoStr"]))
{
response.End();
}
string echoStr = request.QueryString["echoStr"].ToString();
if (checkSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
response.Write(echoStr);
// response.Output.Write("hello");
response.End();
}
}
return new JsonResult();
}
}
/*
protected void Page_Load(object sender,EventArgs e)
{
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获
取传统context
HttpRequestBase request = context.Request;//定义传统request对象
HttpResponseBase response=context.Response;
if(request.HttpMethod=="POST")
{
string weixin = "";
weixin = PostInput();
if(!string.IsNullOrEmpty(weixin))
{
//HttpResponseMessage ResponseMsg=new HttpResponseMessage():
ResponseMsg(weixin);
}
}
}
private void ResponseMsg(string weixin)
{
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获
取传统context
HttpRequestBase request = context.Request;//定义传统request对象
HttpResponseBase response=context.Response;
XmlDocument doc = new XmlDocument();
doc.LoadXml(weixin);//读取xml字符串
XmlElement root = doc.DocumentElement;//将元素放入root
XmlNode MsgType = root.SelectSingleNode("MsgType");
string messageType = MsgType.InnerText;// 获得这个消息类型
try
{
switch(messageType)
{
case "text":
response.Output.Write("hello");
break;
}
}
catch
{
}
finally
{
}
// throw new NotImplementedException();
}
///
/// 获取post请求数据
///
///
private string PostInput()
{
//获取字符流
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
//读取字符流放入数组 并返回
s.Read(b, 0, (int)s.Length);
return Encoding.UTF8.GetString(b);
}
}
}
*/
}
}