一.创建一个空的api项目
二.应用阿里的短信包 aliyun-net-sdk-core
三.登录阿里添加签名和模板
四.创建创建AccessKey
注意 AccessKey创建后,无法再通过控制台查看。一直要下载下来保存。
五.生成接口代码
填入相关信息直接生成代码
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Microsoft.AspNetCore.Mvc; namespace NoteDemo.Controllers { [Route"api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult<string> Get) { var msg = ""; //注意刚刚下载的AccessKey的excel中的accessKeyId和accessSecret填入 IClientProfile profile = DefaultProfile.GetProfile"default", "<accessKeyId>", "<accessSecret>"); DefaultAcsClient client = new DefaultAcsClientprofile); CommonRequest request = new CommonRequest); request.Method = MethodType.POST; request.Domain = "dysmsapi.aliyuncs.com"; request.Version = "2017-05-25"; request.Action = "SendSms"; // request.Protocol = ProtocolType.HTTP; request.AddQueryParameters"PhoneNumbers", "手机号"); request.AddQueryParameters"SignName", "签名"); request.AddQueryParameters"TemplateCode", "模板"); // request.Protocol = ProtocolType.HTTP; try { CommonResponse response = client.GetCommonResponserequest); msg=System.Text.Encoding.Default.GetStringresponse.HttpResponse.Content); } catch ServerException e) { msg = e.ErrorMessage; } catch ClientException e) { msg = e.ErrorMessage; } return msg; } } }
直接运行即可localhost:52374/api/Values
前端调用,直接调用该接口地址即可。