tnblog
首页
视频
资源
登录
不帅~~但是很暖心.....
排名
13
文章
64
粉丝
20
评论
30
申请别的接口数据(网络接口)
是伍尚金哇 : 敲一夜代码,流下两三行泪水,掏空四肢五体,六杯白开水七桶泡面
mui框架-移动端跳转以及传值的简单方法(修改解决方法)
是伍尚金哇 : 测试了 可以直接在 extras: { userid:'10' //自定义扩展...
数据库的varchar和nvarchar的区别
是伍尚金哇 : 没人看 自己看一个 温习一下
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

简单创建微信公众菜单

4394人阅读 2019/12/12 14:10 总访问:388960 评论:0 收藏:0 手机
分类: 微信开发

分别准备两个实体类(DTO):方便构造json对象

需要用到两个引用包,没有的可以在线执行命令install-package 包名 下载

  1. using Newtonsoft.Json;
  2. using System.Net.Http;

1.主菜单DTO

  1. public class WxMenuDTO
  2.     {
  3.         public string type { get; set; }
  4.         public string name { get; set; }
  5.         public string key { get; set; }
  6.         public string url { get; set; }
  7.         public IEnumerable<Sub_Button> sub_button { get; set; }
  8.     }

2.子菜单DTO

  1. public class Sub_Button
  2.     {
  3.         public string type { get; set; }
  4.         public string name { get; set; }
  5.         public string key { get; set; }
  6.         public string url { get; set; }
  7.     }

3.控制器

  1. public ActionResult Index()
  2.         {
  3.            //获取token
  4.                         Access_Token_Tool access_Token_Tool = new Access_Token_Tool();
  5.                         string access_token = access_Token_Tool.getAccess_Token();
  6.             
  7.             
  8.                         //自定义菜单(一级菜单)
  9.                         List<WxMenuDTO> wxMenuDTOs = new List<WxMenuDTO>();
  10.                         wxMenuDTOs.Add(new WxMenuDTO
  11.                         {
  12.                             name = "后端",
  13.                             sub_button = new List<WxMenuDTO>() {//二级菜单
  14.                             new WxMenuDTO(){ type = "click", name = "EF", key = "go_ef", url = null },
  15.                             new WxMenuDTO(){ type = "click", name = "MVC", key = "go_mvvc", url = null },
  16.                             new WxMenuDTO(){ type = "click", name = "Core", key = "go_core", url = null },
  17.                             new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_hmoreandmore", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
  18.                         }
  19.                         });
  20.                         wxMenuDTOs.Add(new WxMenuDTO
  21.                         {
  22.                             name = "前端",
  23.                             sub_button = new List<WxMenuDTO>() {//二级菜单
  24.                             new WxMenuDTO(){ type = "click", name = "JavaScript", key = "go_javacript", url = null },
  25.                             new WxMenuDTO(){ type = "click", name = "jQuery", key = "go_jquery", url = null },
  26.                             new WxMenuDTO(){ type = "click", name = "CSS", key = "go_css", url = null },
  27.                              new WxMenuDTO(){ type = "click", name = "HTML", key = "go_html", url = null },
  28.                             new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_qmoerandmoer", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
  29.                         }
  30.                         });
  31.                         wxMenuDTOs.Add(new WxMenuDTO
  32.                         {
  33.                             name = "数据库",
  34.                             sub_button = new List<WxMenuDTO>() {//二级菜单
  35.                             new WxMenuDTO(){ type = "click", name = "MySQL", key = "go_mysql", url = null },
  36.                             new WxMenuDTO(){ type = "click", name = "SqlServer", key = "go_sqlserver", url = null },
  37.                             new WxMenuDTO(){ type = "click", name = "Oracle", key = "go_oracle", url = null },
  38.                              new WxMenuDTO(){ type = "scancode_push", name = "扫一扫", key = "go_sucandb", url = null },
  39.                             new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_dbmoerandmoer", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
  40.                         }
  41.                         });
  42.             
  43.             
  44.                         //添加button
  45.                         var tempobj = new { button = wxMenuDTOs };
  46.             
  47.                         //设置json忽略空值
  48.                         JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
  49.                         jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
  50.             
  51.                         //序列化成字符串
  52.                         string json = JsonConvert.SerializeObject(tempobj, jsonSerializerSettings);
  53.                         //添加参数(post的参数)
  54.                         StringContent stringContent = new StringContent(json);
  55.             
  56.                         //调用自定义菜单接口
  57.                         HttpClient httpClient = new HttpClient();
  58.                         string result = httpClient.PostAsync("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token, stringContent).Result.Content.ReadAsStringAsync().Result;
  59.             
  60.                         return Json(result, JsonRequestBehavior.AllowGet);
  61.         }


评价
逆而顺之,顺而逆之。