Twilio.AspNet.Core
6.0.0-alpha2
Previous versions contain bugs and vulnerabilities. See CHANGELOG.md for more details.
See the version list below for details.
dotnet add package Twilio.AspNet.Core --version 6.0.0-alpha2
NuGet\Install-Package Twilio.AspNet.Core -Version 6.0.0-alpha2
<PackageReference Include="Twilio.AspNet.Core" Version="6.0.0-alpha2" />
paket add Twilio.AspNet.Core --version 6.0.0-alpha2
#r "nuget: Twilio.AspNet.Core, 6.0.0-alpha2"
// Install Twilio.AspNet.Core as a Cake Addin #addin nuget:?package=Twilio.AspNet.Core&version=6.0.0-alpha2&prerelease // Install Twilio.AspNet.Core as a Cake Tool #tool nuget:?package=Twilio.AspNet.Core&version=6.0.0-alpha2&prerelease
Twilio helper library for ASP.NET
The Twilio helper library for ASP.NET (Twilio.AspNet), helps you integrate the official Twilio SDK for C# and .NET into your ASP.NET applications. The library supports ASP.NET MVC on .NET Framework and ASP.NET Core.
You only need this library if you wish to respond to Twilio webhooks for voice calls and SMS messages. If you only need to use the Twilio REST API's, then you only need the Twilio SDK for C# and .NET.
Twilio.AspNet.Core
Requirements
Requires .NET (Core) 2.0 or later.
Installation
Run the following command to install the package using the .NET CLI:
dotnet add package Twilio.AspNet.Core
Alternatively, from the Package Manager Console or Developer PowerShell, run the following command to install the latest version:
Install-Package Twilio.AspNet.Core
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for Twilio.AspNet.Core and install the package.
Twilio.AspNet.Mvc
Requirements
Requires .NET 4.6.2 or later.
Installation
From the Package Manager Console or Developer PowerShell, run the following command to install the latest version:
Install-Package Twilio.AspNet.Mvc
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for Twilio.AspNet.Mvc and install the package.
Code Samples for either Library
Incoming SMS
using Twilio.AspNet.Common;
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;
public class SmsController : TwilioController
{
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return TwiML(response);
}
}
This controller will handle the SMS webhook. The details of the incoming SMS will be bound to the SmsRequest request
parameter.
By inheriting from the TwilioController
, you get access to the TwiML
method which you can use to respond with TwiML.
Incoming Voice Call
using Twilio.AspNet.Common;
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;
public class VoiceController : TwilioController
{
// GET: Voice
public TwiMLResult Index(VoiceRequest request)
{
var response = new VoiceResponse();
response.Say($"Welcome. Are you from {request.FromCity}?");
return TwiML(response);
}
}
This controller will handle the Voice webhook. The details of the incoming voice call will be bound to the VoiceRequest request
parameter.
By inheriting from the TwilioController
, you get access to the TwiML
method which you can use to respond with TwiML.
Using TwiML
extension methods instead of inheriting from TwilioController
If you can't inherit from the TwilioController
class, you can use the TwiML
extension methods.
using Microsoft.AspNetCore.Mvc; // or System.Web.Mvc for .NET Framework
using Twilio.AspNet.Common;
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;
public class SmsController : Controller
{
// GET: Sms
public TwiMLResult Index(SmsRequest request)
{
var response = new MessagingResponse();
response.Message(
$"Hey there {request.From}! " +
"How 'bout those Seahawks?"
);
return this.TwiML(response);
}
}
This sample is the same as the previous SMS webhook sample, but instead of inheriting from TwilioController
, the SmsController
inherits from the ASP.NET MVC provided Controller
, and uses this.TwiML
to use the TwiML
extension method.
Minimal API
Twilio.AspNet.Core also has support for Minimal APIs.
This sample shows you how you can hande an SMS webhook using HTTP GET and POST.
using Microsoft.AspNetCore.Mvc;
using Twilio.AspNet.Core.MinimalApi;
using Twilio.TwiML;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/sms", ([FromQuery] string from) =>
{
var response = new MessagingResponse();
response.Message($"Ahoy {from}!");
return Results.Extensions.TwiML(response);
});
app.MapPost("/sms", async (HttpRequest request) =>
{
var form = await request.ReadFormAsync();
var from = form["from"];
response.Message($"Ahoy {from}!");
return Results.Extensions.TwiML(response);
});
app.Run();
In traditional MVC controllers, the SmsRequest
, VoiceRequest
, and other typed request object would be bound, but Minimal APIs does not support the same model binding.
Instead, you can bind individual parameters for HTTP GET requests using the FromQuery
attribute. When you don't specify the FromQuery attribute, multiple sources will be considered to bind from in addition to the query string parameters. For HTTP POST requests you can grab the form and then retrieve individual parameters by string index.
To respond with TwiML, use the Results.Extensions.TwiML
method.
Model Bind webhook requests to typed .NET objects
Twilio.AspNet comes with multiple classes to help you bind the data from webhooks to strongly typed .NET objects. Here's the list of classes:
SmsRequest
: Holds data for incoming SMS webhook requestsSmsStatusCallbackRequest
: Holds data for tracking the delivery status of an outbound Twilio SMS or MMSStatusCallbackRequest
: Holds data for tracking the status of an outbound Twilio Voice CallVoiceRequest
: Holds data for incoming Voice Calls
Note: Only MVC Controllers and Razor Pages supports model binding to typed .NET objects. In Minimal APIs and other scenario's, you'll have to write code to extract the parameters yourself.
The following sample shows how to accept inbound SMS, respond, and track the status of the SMS response.
using Twilio.AspNet.Common;
using Twilio.AspNet.Core; // or .Mvc for .NET Framework
using Twilio.TwiML;
public class SmsController : TwilioController
{
private readonly ILogger<SmsController> logger;
public SmsController(ILogger<SmsController> logger)
{
this.logger = logger;
}
public TwiMLResult Index(SmsRequest request)
{
var messagingResponse = new MessagingResponse();
messagingResponse.Message(
body: $"Hey there {request.From}! How 'bout those Seahawks?",
action: new Uri("/Sms/StatusCallback"),
method: Twilio.Http.HttpMethod.Post
);
return TwiML(messagingResponse);
}
public void StatusCallback(SmsStatusCallbackRequest request)
{
logger.LogInformation("SMS Status: {Status}", request.MessageStatus);
}
}
As shown in the sample above, you can add an SmsRequest
as a parameter, and MVC will bind the object for you.
The code then responds with an SMS with the status
and method
parameter. When the status of the SMS changes, Twilio will send an HTTP POST request to StatusCallback
action. You can add an SmsStatusCallbackRequest
as a parameter, and MVC will bind the object for you.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Twilio (>= 5.77.0)
- Twilio.AspNet.Common (>= 6.0.0-alpha2)
-
net5.0
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Twilio (>= 5.77.0)
- Twilio.AspNet.Common (>= 6.0.0-alpha2)
-
net6.0
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Twilio (>= 5.77.0)
- Twilio.AspNet.Common (>= 6.0.0-alpha2)
NuGet packages (15)
Showing the top 5 NuGet packages that depend on Twilio.AspNet.Core:
Package | Downloads |
---|---|
BlueBox.Core
Core Framework |
|
OneSixOneStudios.Abstractions
Package Description |
|
DanSaul.SharedCode
Package Description |
|
Pact.Sms
📲 SMS services |
|
AeonPass.SharedKernel
Package Description |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Twilio.AspNet.Core:
Repository | Stars |
---|---|
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
|
|
meteatamel/knative-tutorial
A collection of samples for Knative Serving, Knative Eventing and Knative-GCP projects.
|
Version | Downloads | Last updated | |
---|---|---|---|
8.0.2 | 1,228,567 | 5/11/2023 | |
8.0.1 | 22,045 | 5/2/2023 | |
8.0.1-rc | 706 | 4/21/2023 | |
8.0.0 | 248,057 | 3/2/2023 | |
7.0.0 | 281,956 | 11/18/2022 | |
7.0.0-rc | 710 | 11/17/2022 | |
6.0.0 | 792,595 | 8/5/2022 | |
6.0.0-alpha2 | 694 | 8/4/2022 | |
6.0.0-alpha | 765 | 7/20/2022 | |
5.77.0 | 163,738 | 7/19/2022 | |
5.73.0 | 322,390 | 4/5/2022 | |
5.71.0 | 143,848 | 2/24/2022 | |
5.68.3 | 362,712 | 11/24/2021 | |
5.37.2 | 844,903 | 11/6/2020 | |
5.33.1 | 806,860 | 10/16/2019 | |
5.20.1 | 473,170 | 10/26/2018 | |
5.9.7 | 182,384 | 3/2/2018 | |
5.8.3 | 70,196 | 11/10/2017 |
Refer to the changelog at https://github.com/twilio-labs/twilio-aspnet/blob/main/CHANGELOG.md