public ActionResult Get()
 {
     string token = Tools.GetToken();
     string res = MenuHelper.Get(token);
     Menu menu = new Menu();
     if (res != "error")
     {
         JObject Jobj = JObject.Parse(res);
         menu.button = ((JArray)Jobj["menu"]["button"]).ToWeChatMenuButtonList().ToList();
     }
     return Content(res + "<br/>" + menu.GetJson().ToString());
 }
        public ActionResult Set()
        {
            string token = Tools.GetToken();

            Menu menu = new Menu();
            menu.button = new List<Button>();
            menu.button.Add(new ClickButton { Key = "K001", Name = "测试菜单1" });
            menu.button.Add(new ClickButton { Key = "K002", Name = "测试菜单2" });
            menu.button.Add(new ClickButton { Key = "K003", Name = "测试菜单3" });

            string res = MenuHelper.Create(token, menu);
            return Content("done:" + res);
        }
        public void CreateMenuIntoJson()
        {
            string except = @"{'button':[
     {	
          'type':'click',
          'name':'今日歌曲',
          'key':'V1001_TODAY_MUSIC'
      },
      {
           'type':'click',
           'name':'歌手简介',
           'key':'V1001_TODAY_SINGER'
      },
      {
           'name':'菜单',
           'sub_button':[
           {	
               'type':'view',
               'name':'搜索',
               'url':'http://www.soso.com/'
            },
            {
               'type':'view',
               'name':'视频',
               'url':'http://v.qq.com/'
            },
            {
               'type':'click',
               'name':'赞一下我们',
               'key':'V1001_GOOD'
            }]
       }]
 }".Replace("\r\n", "").Replace("\t", "").Replace(" ", "").Replace("'", "\"");

            Menu menu = new Menu();
            menu.button = new List<Button>();
            menu.button.Add(new ClickButton { Key = "V1001_TODAY_MUSIC", Name = "今日歌曲" });
            menu.button.Add(new ClickButton { Key = "V1001_TODAY_SINGER", Name = "歌手简介" });
            HasSubButton sub = new HasSubButton();
            sub.Name = "菜单";
            sub.SubButton = new List<Button>();
            sub.SubButton.Add(new ViewButton { Name = "搜索", Url = "http://www.soso.com/" });
            sub.SubButton.Add(new ViewButton { Name = "视频", Url = "http://v.qq.com/" });
            sub.SubButton.Add(new ClickButton { Name = "赞一下我们", Key = "V1001_GOOD" });
            menu.button.Add(sub);

            string ret = menu.GetJson().ToString().Replace("\r\n", "").Replace("\t", "").Replace(" ", "");
            Assert.AreEqual(except, ret);
        }
 /// <summary>
 /// 创建自定义菜单
 /// </summary>
 /// <param name="AccessToken"></param>
 /// <returns></returns>
 public static string Create(string AccessToken, Menu menu)
 {
     string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", AccessToken);
     return Utils.RequestPost(url, menu.GetJson().ToString());
 }
        public void Json2Menu()
        {
            string jsonData = @"
{
    'menu': {
        'button': [
            {
                'type': 'click',
                'name': '今日歌曲',
                'key': 'V1001_TODAY_MUSIC',
                'sub_button': []
            },
            {
                'type': 'click',
                'name': '歌手简介',
                'key': 'V1001_TODAY_SINGER',
                'sub_button': []
            },
            {
                'name': '菜单',
                'sub_button': [
                    {
                        'type': 'view',
                        'name': '搜索',
                        'url': 'http://www.soso.com/',
                        'sub_button': []
                    },
                    {
                        'type': 'view',
                        'name': '视频',
                        'url': 'http://v.qq.com/',
                        'sub_button': []
                    },
                    {
                        'type': 'click',
                        'name': '赞一下我们',
                        'key': 'V1001_GOOD',
                        'sub_button': []
                    }
                ]
            }
        ]
    }
}";
            JObject Jobj = JObject.Parse(jsonData);
            Menu menu = new Menu();
            menu.button = ((JArray)Jobj["menu"]["button"]).ToWeChatMenuButtonList().ToList();

            Assert.IsInstanceOfType(menu.button[0], typeof(ClickButton));
            Assert.IsInstanceOfType(menu.button[1], typeof(ClickButton));
            Assert.IsInstanceOfType(menu.button[2], typeof(HasSubButton));
            HasSubButton sub = (HasSubButton)menu.button[2];
            Assert.IsInstanceOfType(sub.SubButton[0], typeof(ViewButton));
            Assert.IsInstanceOfType(sub.SubButton[1], typeof(ViewButton));
            Assert.IsInstanceOfType(sub.SubButton[2], typeof(ClickButton));
        }