ASP. NET WeChat public account add menu, asp.net letter public add

Source: Internet
Author: User

ASP. NET public account add menu, asp.net letter public add

The examples in this article share the code for adding a menu to the public account for your reference. The details are as follows:

Testjs. aspx code:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "testjs. aspx. cs "Inherits =" MyTest. webUI. manager. main. testjs "%> <! DOCTYPE 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 menu operation assistant class

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 content:

{"Button": [{"type": "click", "name": "Today's songs", "key": "V1001_TODAY_MUSIC" },{ "name ": "menu", "sub_button": [{"type": "view", "name": "Search", "url": "http://www.soso.com /"}, {"type": "view", "name": "video", "url": "http://v.qq.com/" },{ "type": "click", "name ": "like us", "key": "V1001_GOOD"}]}

Running result:

This article has been compiled into ASP. NET development tutorial summary. You are welcome to learn and read it.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.