This article mainly introduces public platform development: using Senparc. Weixin. MPSDK Senparc. Weixin. mp sdk has covered all public APIs of 6.x.
The source code of the entire project and the compiled assembly can be obtained in this project: https://github.com/JeffreySu/WeiXinMPSDK
PS: As the API and Senparc. Weixin SDK are constantly updated, the latest code and the source code in github above the Demo shall prevail.
Now we have created an ASP.. net mvc project. let's take a look at how to interact with the project. (The same is true for Webforms, except that the Action in Controller is replaced. ).
The source code of the project to be demonstrated can also be found in the open-source project (because there must be two projects in the source code and one public project, the structure is slightly different from the one shown below, and the logic is completely consistent):
MVC: https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Senparc.Weixin.MP.Sample
WebForms: https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Senparc.Weixin.MP.Sample.WebForms
Step 1: Create an empty ASP. net mvc (4.0) project named Senparc. Weixin. MP. Sample
Step 4: modify WeixinController. cs
Add the following code to WeixinController. cs:
Using System; using System. IO; using System. web. configuration; using System. web. mvc; using Senparc. weixin. MP. entities. request; namespace Senparc. weixin. MP. sample. controllers {using Senparc. weixin. MP. mvcExtension; using Senparc. weixin. MP. sample. commonService. customMessageHandler; public partial class WeixinController: Controller {public static readonly string Token = "YourToken"; // consistent with the Token settings of the public account background, zone Case sensitive. Public static readonly string EncodingAESKey = "YourKey"; // This parameter is case sensitive and consistent with EncodingAESKey settings in the public account background. Public static readonly string AppId = "YourAppId"; // This parameter is case sensitive and consistent with the AppId settings of the public account background. ////// Backend authentication address (use Get), enter the "interface configuration information" Url in the background such as: http://weixin.senparc.com/weixin ///[HttpGet] [ActionName ("Index")] public ActionResult Get (PostModel postModel, string echostr) {if (CheckSignature. check (postModel. signature, postModel. timestamp, postModel. nonce, Token) {return Content (echostr); // if a random string is returned, the verification succeeds.} else {return Content ("failed:" + postModel. signature + "," + MP. checkSignature. getSignature (postModel. timestamp, postModel. nonce, Token) + ". "+" If you see this sentence in the browser, it indicates that this address can be used as the Url of the public account background. please ensure that the Token is consistent. ");}}////// After the user sends a message, the platform automatically posts a request here and waits for the XML response. /// PS: This method is a simplified method with the same effect as OldPost. /// Versions later than v0.8 can be combined with the Senparc. Weixin. MP. MvcExtension extension package to use WeixinResult. for details, refer to the MiniPost method. ///[HttpPost] [ActionName ("Index")] public ActionResult Post (PostModel postModel) {if (! CheckSignature. Check (postModel. Signature, postModel. Timestamp, postModel. Nonce, Token) {return Content ("Parameter error! ");} PostModel. token = Token; // maintain consistent postModel according to your background settings. encodingAESKey = EncodingAESKey; // keep the postModel consistent with your background settings. appId = AppId; // keep consistent with your background settings // customize MessageHandler, where the detailed judgment operations on the request are all included. Var messageHandler = new CustomMessageHandler (Request. inputStream, postModel); // receives messageHandler messages. execute (); // return new FixWeixinBugWeixinResult (messageHandler); // return result }}}
The first Get corresponds to the request when URL is set in the background, and the second Post is used to accept customer requests forwarded.
CustomMessageHandler is a class created by ourselves for implementing MessageHandler (for details about MessageHandler, see Senparc. weixin. mp sdk public platform development tutorial (6): Learn about MessageHandler, you can also see here), all the core logic for message processing is included in CustomMessageHandler for execution. In addition to processing the response, MessageHandler also provides functions such as processing the context of a single user conversation, which is very convenient.
So far, the entire Senparc. Weixin. mp sdk has been basically developed and can be directly released, and "advanced functions" have been used in the background.
For example, enter http: // xxx/Weixin as the Url and weixin as the Token.
For more public platform development, see Senparc. Weixin. mp sdk!