排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256
50010702506256
欢迎加群交流技术
分类:
微信开发
分别准备两个实体类(DTO):方便构造json对象
需要用到两个引用包,没有的可以在线执行命令install-package 包名 下载
using Newtonsoft.Json; using System.Net.Http;
1.主菜单DTO
public class WxMenuDTO
{
public string type { get; set; }
public string name { get; set; }
public string key { get; set; }
public string url { get; set; }
public IEnumerable<Sub_Button> sub_button { get; set; }
}2.子菜单DTO
public class Sub_Button
{
public string type { get; set; }
public string name { get; set; }
public string key { get; set; }
public string url { get; set; }
}3.控制器
public ActionResult Index()
{
//获取token
Access_Token_Tool access_Token_Tool = new Access_Token_Tool();
string access_token = access_Token_Tool.getAccess_Token();
//自定义菜单(一级菜单)
List<WxMenuDTO> wxMenuDTOs = new List<WxMenuDTO>();
wxMenuDTOs.Add(new WxMenuDTO
{
name = "后端",
sub_button = new List<WxMenuDTO>() {//二级菜单
new WxMenuDTO(){ type = "click", name = "EF", key = "go_ef", url = null },
new WxMenuDTO(){ type = "click", name = "MVC", key = "go_mvvc", url = null },
new WxMenuDTO(){ type = "click", name = "Core", key = "go_core", url = null },
new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_hmoreandmore", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
}
});
wxMenuDTOs.Add(new WxMenuDTO
{
name = "前端",
sub_button = new List<WxMenuDTO>() {//二级菜单
new WxMenuDTO(){ type = "click", name = "JavaScript", key = "go_javacript", url = null },
new WxMenuDTO(){ type = "click", name = "jQuery", key = "go_jquery", url = null },
new WxMenuDTO(){ type = "click", name = "CSS", key = "go_css", url = null },
new WxMenuDTO(){ type = "click", name = "HTML", key = "go_html", url = null },
new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_qmoerandmoer", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
}
});
wxMenuDTOs.Add(new WxMenuDTO
{
name = "数据库",
sub_button = new List<WxMenuDTO>() {//二级菜单
new WxMenuDTO(){ type = "click", name = "MySQL", key = "go_mysql", url = null },
new WxMenuDTO(){ type = "click", name = "SqlServer", key = "go_sqlserver", url = null },
new WxMenuDTO(){ type = "click", name = "Oracle", key = "go_oracle", url = null },
new WxMenuDTO(){ type = "scancode_push", name = "扫一扫", key = "go_sucandb", url = null },
new WxMenuDTO(){ type = "view", name = "了解更多", key = "go_dbmoerandmoer", url = "http://www.tnblog.net/Shangjin123/article/details/2965" }
}
});
//添加button
var tempobj = new { button = wxMenuDTOs };
//设置json忽略空值
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
//序列化成字符串
string json = JsonConvert.SerializeObject(tempobj, jsonSerializerSettings);
//添加参数(post的参数)
StringContent stringContent = new StringContent(json);
//调用自定义菜单接口
HttpClient httpClient = new HttpClient();
string result = httpClient.PostAsync("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token, stringContent).Result.Content.ReadAsStringAsync().Result;
return Json(result, JsonRequestBehavior.AllowGet);
}评价