一.創建一個空的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 DefaultAcsClient(profile); 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.GetCommonResponse(request); msg=System.Text.Encoding.Default.GetString(response.HttpResponse.Content); } catch (ServerException e) { msg = e.ErrorMessage; } catch (ClientException e) { msg = e.ErrorMessage; } return msg; } } }
直接運行即可localhost:52374/api/Values
前端調用,直接調用該接口地址即可。