WeChat基础 senparc公众平台搭建
新建一个MVC项目 清理下项目
引用-NuGet-引用
在WebConfig里面配置
<!-- 微信公众号URL对接信息 --> <add key="WeixinToken" value="xxxxx" /> <add key="WeixinEncodingAESKey" value="xxxxx" /> <!-- 高级接口信息 --> <add key="WeixinAppId" value="xxxxx" /> <add key="WeixinAppSecret" value="xxxxxx" />
在控制器里创建Weixin
1 public static readonly string Token = WebConfigurationManager.AppSettings["WeixinToken"]; 2 public static readonly string EncodingAESKey = WebConfigurationManager.AppSettings["WeixinEncodingAESKey"]; 3 public static readonly string AppId = WebConfigurationManager.AppSettings["WeixinAppId"]; 4 // GET: Weixin 5 [HttpGet] 6 [ActionName("Index")] 7 public ActionResult Get(PostModel postModel, string echostr) 8 { 9 if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) 10 { 11 return Content(echostr); //返回随机字符串则表示验证通过 12 } 13 else 14 { 15 return Content("failed:" + postModel.Signature + "," + CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" + 16 "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"); 17 } 18 } 19 20 [HttpPost] 21 [ActionName("Index")] 22 public ActionResult Post(PostModel postModel) 23 { 24 if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) 25 { 26 return Content("参数错误!"); 27 } 28 29 postModel.Token = Token;//根据自己后台的设置保持一致 30 postModel.EncodingAESKey = EncodingAESKey;//根据自己后台的设置保持一致 31 postModel.AppId = AppId;//根据自己后台的设置保持一致 32 33 //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 34 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel);//接收消息 35 36 messageHandler.Execute();//执行微信处理过程 37 38 return new FixWeixinBugWeixinResult(messageHandler);//返回结果 39 40 }
在写weixinController时 发现CustomMessageHandler没有找到对应的类型 需要引入新项目Senparc.Weixin.MP.Sample.CommonService
基本的处理接收消息的方法都在这里
返回文字
1 var responseMessage = CreateResponseMessage<ResponseMessageText>(); 2 responseMessage.Content = "您点击了view按钮,将打开网页:" + requestMessage.EventKey; 3 return responseMessage;
返回窗口
1 var responseMessage = CreateResponseMessage<ResponseMessageNews>(); 2 responseMessage.Articles.Add(new Article() 3 { 4 Title = "查询信息", 5 Description = "xx"+ query.Location, 6 PicUrl = "lp.jpg", 7 Url = "http://sdk.weixin.senparc.com" 8 }); 9 10 return responseMessage;