下面在 Web 空应用里展现一个简单的例子来实现发送文本消息。web
本文目录:
json
$ dotnet new web --name ASPNETCoreWeixinWorkDemo
dotnet 是程序的名字
new 是一个子程序的名字
web 是要使用的项目模板的名字
--name ASPNETCoreWeixinWorkDemo 指定要建立的项目的名字是 ASPNETCoreWeixinWorkDemo浏览器
$ cd ASPNETCoreWeixinWorkDemo
$ dotnet add package Senparc.Weixin.Work
这个命令的执行效果能够在 WeixinWorkDemo.csproj 文件中看到。缓存
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Senparc.Weixin.Work" Version="3.7.104.2" /> </ItemGroup> </Project>
aappsettings.Development.json 文件通常用做 ASP.NET Core 项目的开发环境配置文件,在 ASP.NET Core 项目中一般能够看到。微信
文件内容以下,其中须要替换你本身的信息进去。app
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "SenparcSetting": { "IsDebug": true, "DefaultCacheNamespace": "DefaultCache" }, "SenparcWeixinSetting": { "IsDebug": true, "WeixinCorpId": "替换为你的企业微信企业ID", "WeixinCorpAgentId": "替换为你的企业微信应用ID", "WeixinCorpSecret": "替换为你的企业微信应用的Secret" } }
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddControllers();//注册控制器 services.AddMemoryCache();//注册本地缓存 services.AddSenparcWeixinServices(Configuration);//注册全局微信服务 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); endpoints.MapControllers();//设置路由匹配 }); app.UseSenparcGlobal(env, senparcSetting.Value, globalRegister => { }) .UseSenparcWeixin(senparcWeixinSetting.Value, weixinRegister => { weixinRegister.RegisterWorkAccount(senparcWeixinSetting.Value, "替换为你的应用名字");//注册企业微信 }); } }
在项目下添加Controller目录,在目录下添加SendMessageController.cs,添加内容以下async
using Microsoft.AspNetCore.Mvc; using Senparc.Weixin; using Senparc.Weixin.Work.AdvancedAPIs; using Senparc.Weixin.Work.Containers; namespace ASPNETCoreWeixinWorkDemo.Controllers { [ApiController] [Route("[controller]")] public class SendMessageController : ControllerBase { static readonly string CorpId = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpId;//经过全局对象获取配置 static readonly string CorpSecret = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpSecret;//经过全局对象获取配置 static readonly string AppId = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpAgentId;//经过全局对象获取配置 static readonly string AppKey = AccessTokenContainer.BuildingKey(CorpId, CorpSecret);//用于获取token的标识 // GET [HttpGet] public IActionResult Get() { // 经过标识获取 access token var token = AccessTokenContainer.GetToken(AppKey); // 使用 SDK 的消息 API 发送文本信息 MassApi.SendText(token, AppId, "Hello World!", "替换为要发送的人员帐号"); return Ok("Send Message To OK."); } } }
而后在浏览器里访问 https://localhost:5001/SendMessage,就能够看到页面显示“Send Message To OK.”,在企业微信客户端里就能够看到“Hello World!”消息了。ui