程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> ASP.NET微信公眾號添加菜單,asp.net信公眾添加

ASP.NET微信公眾號添加菜單,asp.net信公眾添加

編輯:關於.NET

ASP.NET微信公眾號添加菜單,asp.net信公眾添加


本文實例為大家分享了微信公眾號添加菜單的具體代碼,供大家參考,具體內容如下

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微信開發教程匯總》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved