欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET微信公众号添加菜单

程序员文章站 2024-03-30 21:24:51
本文实例为大家分享了微信公众号添加菜单的具体代码,供大家参考,具体内容如下 testjs.aspx代码: <%@ page language="c#" a...

本文实例为大家分享了微信公众号添加菜单的具体代码,供大家参考,具体内容如下

testjs.aspx代码:

<%@ page language="c#" autoeventwireup="true" codebehind="testjs.aspx.cs" inherits="mytest.webui.manager.main.testjs" %>

<!doctype html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta http-equiv="content-type" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
 <title></title>
 <script type="text/javascript" src="../../js/jweixin-1.0.0.js"></script>
 <script type="text/javascript" src="../../js/jquery-1.12.2.min.js"></script>
 <style>
  .btn {
   width: 100%;
  }
 </style>
</head>

<body>
 <form id="form1" runat="server">
  <div>
   <asp:label id="lblmsg" runat="server" text=""></asp:label>

   <asp:button cssclass="btn" id="button1" runat="server" text="添加菜单" onclick="button1_click" /><br />
   <asp:button cssclass="btn" id="button2" runat="server" text="删除菜单" onclick="button2_click" /><br /> 
  </div>
 </form>
</body>
</html>

 public partial class testjs : system.web.ui.page
 {
  protected void page_load(object sender, eventargs e)
  {
  }

  protected void button1_click(object sender, eventargs e)
  {
   menuoperator mm = new menuoperator();

   lblmsg.text=mm.menu_add();
  }

  protected void button2_click(object sender, eventargs e)
  {
   menuoperator mm = new menuoperator();

   lblmsg.text = mm.menu_del();
  }
 }

menuoperator.cs菜单操作助手类

 public class menuoperator
 {

  public string menu_add() {
   string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
   string token = mainarg.get_token();
   url += token;
   httpwebrequest req =(httpwebrequest) httpwebrequest.create(url);
   req.method = "post";//大小写不限
   string menupath = httpcontext.current.server.mappath(@"~/templepath/menu.txt");
   stream ms =file.open(menupath, filemode.openorcreate);
   byte[] paramb = new byte[ms.length];
   ms.read(paramb, 0, (int32)ms.length);
   string param = encoding.default.getstring(paramb);
   ms.close();
   using (streamwriter stream=new streamwriter(req.getrequeststream()))
   { 
    stream.write(param);
   }
   using (httpwebresponse response=(httpwebresponse)req.getresponse())
   {
    stream s = response.getresponsestream();
    streamreader sr = new streamreader(s);
    string result = sr.readtoend();
    sr.close();

    return result; 
   }


  }

  public string menu_del() {
   string token = mainarg.get_token();
   string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token; ;
   httpwebrequest req = (httpwebrequest)httpwebrequest.create(url);
   using (httpwebresponse response =(httpwebresponse) req.getresponse()) {
    streamreader sr = new streamreader(response.getresponsestream());
    string result = sr.readtoend();
    sr.close();
    return result;
   }

  }
 }

menu.txt内容:

{
  "button":[
  { 
   "type":"click",
   "name":"今日歌曲",
   "key":"v1001_today_music"
  },
  {
   "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"
   }]
  }]
 }

运行结果如图:

ASP.NET微信公众号添加菜单

ASP.NET微信公众号添加菜单

本文已被整理到了《asp.net微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。