这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Add SMS Controller

Right-click on the Controllers Folder and select add->Controller. Select "Add Empty MVC Controller" and name it SmsController.

Add using statements for Vonage.Messaging, Vonage.Request, and Microsoft.Extensions.Configuration at the top of this file.

Inject Configuration

Dependency inject an IConfiguration object via the constructor like so:

public IConfiguration Configuration { get; set; }

public SmsController(IConfiguration config)
{
    Configuration = config;
}

Add Send SMS Action

Next, add a Send SMS Action to the controller:

[HttpPost]
public IActionResult Sms(Models.SmsModel sendSmsModel)
{
    if (ModelState.IsValid)
    {
        try
        {
            var VONAGE_API_KEY = Configuration["VONAGE_API_KEY"];
            var VONAGE_API_SECRET = Configuration["VONAGE_API_SECRET"];
            var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
            var client = new SmsClient(credentials);
            var request = new SendSmsRequest { To = sendSmsModel.To, From = sendSmsModel.From, Text = sendSmsModel.Text };
            var response = client.SendAnSms(request);
            ViewBag.MessageId = response.Messages[0].MessageId;
        }
        catch(VonageSmsResponseException ex)
        {
            ViewBag.Error = ex.Message;
        }
    }
    return View("Index");
}

How to Receive SMS Delivery Receipts with ASP.NET Core MVC

Delivery receipts allow you to get information about when an SMS is delivered to a user's handset. This tutorial shows how you can receive these delivery receipt notifications in your ASP .NET application.



Steps