下面在 Web 空应用里展示一个简单的例子来实现发送文本消息。

本文目录:

创建 Web 空应用

命令行方式创建

$ dotnet new web --name ASPNETCoreWeixinWorkDemo

dotnet 是程序的名字

new 是一个子程序的名字

web 是要使用的项目模板的名字

--name ASPNETCoreWeixinWorkDemo 指定要创建的项目的名字是 ASPNETCoreWeixinWorkDemo

添加SDK引用

命令行方式

进入项目目录
$ 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>

配置和使用SDK

添加appsettings.Development.json文件

aappsettings.Development.json 文件一般用作 ASP.NET Core 项目的开发环境配置文件,在 ASP.NET Core 项目中通常可以看到。

文件内容如下,其中需要替换你自己的信息进去。

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"SenparcSetting": {
"IsDebug": true,
"DefaultCacheNamespace": "DefaultCache"
},
"SenparcWeixinSetting": {
"IsDebug": true,
"WeixinCorpId": "替换为你的企业微信企业ID",
"WeixinCorpAgentId": "替换为你的企业微信应用ID",
"WeixinCorpSecret": "替换为你的企业微信应用的Secret"
}
}

修改Startup.cs,配置服务

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,在Get方法中发送消息

在项目下添加Controller目录,在目录下添加SendMessageController.cs,添加内容如下

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!”消息了。

【原创】在 ASP.NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息的更多相关文章

  1. 【原创】在 .NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息

    下面在控制台应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建控制台应用 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appsettings.json文件 ...

  2. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  3. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  5. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  6. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

  7. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  8. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  9. 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目

    今天,Visual Studio中没有内置工具来测试WEB API.使用浏览器,只能测试http GET请求.您需要使用Postman,SoapUI,Fiddler或Swagger等第三方工具来执行W ...

随机推荐

  1. 洛谷$P$2123 皇后游戏 贪心

    正解:贪心 解题报告: 传送门! 心血来潮打算把$luogu$提高历练地及其之前的所有专题都打通关,,,$so$可能会写一些比较水的题目的题解$QAQ$ 这种题,显然就套路地考虑交换相邻两个人的次序的 ...

  2. Google被墙怎么办?

    Google被墙怎么办? 1 声明 请小伙伴们遵守法律法规,我们只是为了更好的查询学习资料. 想使用Google查询相关资料 想使用Google账号管理收藏夹 想使用Google商店安装软件 == 2 ...

  3. License for package Android SDK Build-Tools 28.0.3 not accepted

    License for package Android SDK Build-Tools 28.0.3 not accepted 用flutter进行编写时出现了标题的错误,不是配置的原因,而是需要接受 ...

  4. 由数据迁移至MongoDB导致的数据不一致问题及解决方案

    故事背景 企业现状 2019年年初,我接到了一个神秘电话,电话那头竟然准确的说出了我的昵称:上海小胖. 我想这事情不简单,就回了句:您好,我是小胖,请问您是? "我就是刚刚加了你微信的 xx ...

  5. 《图解机器学习-杉山将著》读书笔记---CH4

    CH4 带有约束条件的最小二乘法 重点提炼 提出带有约束条件的最小二乘学习法的缘故:   左图中可见:一般的最小二乘学习法有个缺点----对于包含噪声的学习过程经常会过拟合 右图:有了空间约束之后,学 ...

  6. python之嵌套函数调用

    #定义嵌套函数 def func1(): print('this is func1') def func2(): print('this is func2')#调用1func1()输出:this is ...

  7. Java操作Jxl实现数据交互。三部曲——《第三篇》

    Java操作Jxl实现上传文本文件实现转PDF格式在线预览. 本文实现背景Web项目:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:S ...

  8. input 只允许输入小数

    oninput = "value=value.replace(/[^\d]/g,'')" 输入浮点数不好使 突发奇想自己写一个与众不同的... oninput="valu ...

  9. [Golang] 剑走偏锋 -- IoComplete ports

    前言 Golang 目前的主要應用領域還是後臺微服務,雖然在業務領域也有所應用但仍然是比較小衆的選擇.大多數的服務運行環境都是linux,而在windows中golang應用更少,而作者因爲特殊情況, ...

  10. js-xlsx 一个实用的js 导出列表插件

    在前端开发过程中,导出列表功能的开发无非两种,一种是有后台生成,发生给前端下载,第二种是前端进行列表的导出工作.之前接触了一种前端导出列表的插件 tableExport.js ,但是其缺点很明显,需要 ...