微信--自定义菜单(创建和删除)

微信自定义菜单这个就比较简单,直接贴代码。

微信说的注意:

目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。 

 

 1         #region 菜单管理
 2         public ActionResult CreateMenu()
 3         {
 4             string access_token = weixin.AccessToken();
 5             string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token;
 6             string data = "";
 7 
 8             using (StreamReader sr = new StreamReader(Server.MapPath("/Config/menu.txt")))
 9             {
10                 data = sr.ReadToEnd();
11             }
12 
13 
14             var json = HttpHelper.Post(url, data);
15             return Content(json);
16         }
17 
18         public ActionResult DeleteMenu()
19         {
20             string access_token = weixin.AccessToken();
21             string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + access_token;
22 
23             var json = HttpHelper.Get(url);
24             return Content(json);
25         }
26         #endregion

返回结果

正确时的返回JSON数据包如下:

{"errcode":0,"errmsg":"ok"}

错误时的返回JSON数据包如下(示例为无效菜单名长度):

{"errcode":40018,"errmsg":"invalid button name size"}

 

 

 

menu.txt

{
     "button":[
      {
           "name":"一级标题1",
           "sub_button":[
           {    
               "type":"view",
               "name":"二级标题1",
               "url":"http://xx.xx.com/1.html"
            },
            {
               "type":"view",
               "name":"二级标题2",
               "url":""
            },
            {
               "type":"view",
               "name":"二级标题3",
               "url":""
            },
            {
               "type":"view",
               "name":"二级标题4",
               "url":""
            },
            {
               "type":"view",
               "name":"二级标题5",
               "url":""
            }]
       },
       {
           "type":"view",
           "name":"一级标题2",
           "url":"http://xx.xx.com/2.html"
       },
       {
           "name":"一级标题3",
           "sub_button":[
            {
               "type":"view",
               "name":"二级标题1",
               "url":""
            },
            {
               "type":"view",
               "name":"二级标题2",
               "url":"http://xx.xx.com/3-1.html"
            }]
       }]
 }

 

微信--自定义菜单(创建和删除)

上一篇:C#微信开发之旅--自定义菜单


下一篇:微信--网页授权获取用户基本信息