Creating an ASP.NET MVC Webhook Project (2023)

Webhooks - URLs you expose to handle callbacks - are pivotal to harnessing the power of Twilio in your web application. A powerful concept, webhooks also happen to be very easy to create in your ASP.NET MVC Project. We'll prove it - let's expose some webhooks in ASP.NET MVC and receive some text messages!

Twilio can send your web application an HTTP request when certain events happen, such as an incoming text message to one of your Twilio phone numbers. These requests are called webhooks, or status callbacks. For more, check out our guide to Getting Started with Twilio Webhooks. Find other webhook pages, such as a security guide and an FAQ in the Webhooks section of the docs.

Webhooks and ASP.NET MVC Projects

For one webhook example, when receiving an SMS (text) message, Twilio will call your application with data describing the incoming message. The same process applies for Programmable Voice, Programmable Chat, Programmable Video, and the other Twilio APIs.

This guide will walk you through setting up a basic project for handling incoming webhook requests in your project. The screenshots in the steps shown were made with Visual Studio 2015, but the process is much the same in Visual Studio 2013 and 2017.

Create the Project

Create a new ASP.NET MVC project that can handle incoming requests from Twilio. Open up Visual Studio and select File... New Project. Find the ASP.NET Web Application template for C# as shown here:

Creating an ASP.NET MVC Webhook Project (1)

Name the project and solution to suit your needs.

After clicking OK, you will be prompted for the ASP.NET Project details:

Creating an ASP.NET MVC Webhook Project (2)

An "Empty" project is all you need if you will only be handling webhook requests, but be sure to select the MVC option for adding folders and core references. Finally, select “Host in the cloud” if you want to be able to easily publish your app to Azure. Twilio will need a publicly reachable URL to which to send requests and hosting in Azure fits this bill.

Hosting with Azure

If you select the option to host in Azure, the next dialog to pop up will be the Azure Web App Settings:

Creating an ASP.NET MVC Webhook Project (3)

At this point you will need to login to your Microsoft Azure subscription. Once you’re logged in, you must enter a Web App name that will be globally unique (you will get a green checkmark once you’ve selected a unique name). Keep track of that name for later, as it will be part of the URL for your webhook.

You will likely need to create a new App Service plan and Resource group, unless you’ve already been deploying your own Web Apps to Azure. You can select whatever Region you like and a Database server based on whether you will need one for your app. If you will just be calling other API's, then you likely do not need one.

Once you click OK, Visual Studio will create your new project and publish your app to Azure. You will know it is complete by looking at the "Azure App Service Activity" tab in Visual Studio:

Creating an ASP.NET MVC Webhook Project (4)

NuGet Packages

To finish off the framing of the project, you need to install a few dependencies via the NuGet Package Manager. This can be most quickly accomplished using the Console, which you can reach by choosing the Tools menu, NuGet Package Manager, and Package Manager Console.

(Video) Implementing a Simple SaaS Webhook in .NET

To get the Twilio helper libraries you will need use the following command. It requests the Twilio.AspNet.Mvc package, telling NuGet to get the latest versions of the dependent Twilio package:

Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor

Create a "Hello World" Controller

To explore how to handle webhooks in ASP.NET MVC, this guide will use the Twilio Programmable SMS product. When Twilio receives an SMS message at a phone number that you own it will call your webhook. We can listen for this webhook using an ASP.NET MVC Controller.

Find the “Controllers” folder of your new project in the Solution Explorer:

Creating an ASP.NET MVC Webhook Project (5)

Right-click the folder and select Add... Controller... Choose an empty MVC 5 Controller:

Creating an ASP.NET MVC Webhook Project (6)

Name the controller whatever makes sense for your use case:

Creating an ASP.NET MVC Webhook Project (7)

Update your using statements to import the Twilio namespaces:

using Twilio.AspNet.Common;using Twilio.AspNet.Mvc;using Twilio.TwiML;

Then, change your controller class to inherit from TwilioController instead of the default Controller provided by ASP.NET MVC:

public class HelloWorldController : TwilioController

Next, modify the Index action method to have the [HttpPost] attribute, a return value of TwiMLResult and an SmsRequest parameter:

[HttpPost]public TwiMLResult Index(SmsRequest request)
(Video) Writing webhooks on localhost with Visual Studio and IIS Express

The SmsRequest class is defined in the Twilio.AspNet.Mvc library and will contain all of the deserialized parameters that Twilio will pass to us. If you were responding to a voice call, then you would use the VoiceRequest class.

Twilio expects your webhook to return TwiML (XML), but you don’t need to code the XML by hand. You can make use of the MessagingResponse and VoiceResponse classes (from the Twilio library) to build a TwiML response programmatically. These classes contain methods corresponding to the TwiML verbs that Twilio understands.

Update your action method as follows:

ASP.NET MVC Webhook Example

Example ASP.NET MVC Action that responds to a Twilio SMS webhook.

(Video) Asp.Net Webhooks

After adding the “Hello World” message, call the TwiML function provided by TwilioController. This is analogous to calling View(myModel) in a typical ASP.NET MVC controller action.

Test and Debug Locally

Run your application from within Visual Studio to view your application in the browser. In this case, there isn't a default home page for your app:

Creating an ASP.NET MVC Webhook Project (8)

You might think to add /HelloWorld (or whatever you named your controller) to the URL in the browser. You would be on the right track, but this will still return a 404 because your browser will be doing a GET request instead of a POST request. You can break out a tool like Fiddler or Postman to test POST requests to your newly created controller, but there is a simpler way that’s built right into Windows - PowerShell. Open a new PowerShell window and run the following command:

Invoke-WebRequest http://localhost:XXXXX/HelloWorld -Method POST

Replace the XXXXX with the random port number that Visual Studio assigned to your web app. You can find this in the URL when Visual Studio first launched your app in the browser. Change HelloWorld to whatever you named your controller (without the Controller suffix).

This will return a PowerShell object with the response from your controller:

StatusCode : 200StatusDescription : OKContent : <?xml version="1.0" encoding="utf-8"?> <Response> <Message>Hello World</Message> </Response>RawContent : ...

You should be able to see the raw XML response we are returning in the Content property. Pro-tip: you can see just the Content property with a single command:

(Invoke-WebRequest http://localhost:XXXXX/HelloWorld -Method POST).Content

(Want more detailed information on using PowerShell with HTTP Requests or Twilio? Try our guide to sending HTTP Requests with PowerShell).

Deploy and Test Your ASP.NET MVC App

Publish to Azure

To publish your app to Azure, find the “Azure App Service Activity” tab in Visual Studio and click the Publish button:

(Video) Osiris Tutorial Series - Build webhooks in ASP.NET Web API

Creating an ASP.NET MVC Webhook Project (9)

Once the publish is complete, you can test your app from PowerShell again:

Invoke-WebRequest https://yourapp.azurewebsites.net/HelloWorld -Method POST

Replace yourapp with the name you selected for your Azure Web App and HelloWorld with the name of your controller.

Assuming that works, you can move on to attaching your webhook to a phone number in Twilio. (To learn how to debug your webhook locally without deploying, see our guide on that topic.)

Configure a Twilio Number

If you haven’t already purchased a Twilio phone number, do so now. Once that is done, you can configure the Messaging Webhook like so:

Creating an ASP.NET MVC Webhook Project (10)

Use the same URL for your Azure Web App that you tested in PowerShell earlier. Be sure to click the SAVE button to save your configuration for the phone number.

Test it out! Send any text message to your number and you should get back the ‘Hello World’ response:

Creating an ASP.NET MVC Webhook Project (11)

Conclusion

This walkthrough should get you most of the way down the path to handling webhooks in ASP.NET MVC projects. For more in-depth examples, see the complete applications for a variety of Twilio use cases that are documented in our Tutorials.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

FAQs

Can I create my own webhook? ›

You can install webhooks on an organization or on a specific repository. To set up a webhook, go to the settings page of your repository or organization. From there, click Webhooks, then Add webhook. Alternatively, you can choose to build and manage a webhook through the Webhooks API.

How do I create a webhook application? ›

Create a Webhook
  1. Go to your stack, and click on the “Settings” icon on the left navigation panel.
  2. Click on Webhooks. ...
  3. Click on the + New Webhook button located at the top of the page.
  4. In the Create Webhook page, provide the following webhook details: ...
  5. Click on the Save button.

How do you make a webhook listener? ›

Steps for Execution
  1. Create a content type and add an entry.
  2. Create an AWS Lambda Function.
  3. Create an API Gateway.
  4. Deploy your API.
  5. Create a Webhook in Contentstack.
  6. Try it out.

What is the difference between webhook and API? ›

An application programming interface (API) is a software interface that serves as a bridge between computers and applications. A webhook is a way for one application to deliver data to another app in real-time.

How do I setup a webhook server? ›

CONTENTS
  1. Setup steps. Step 1 - Create a new Node.js project. Step 2 - Create a HTTP server. Step 3 - Return the challenge. Step 4 - Webhook endpoint. Main steps in the verification process: Step 5 - Publish your webhook server. Step 6 - Set up webhook in ChatBot. Step 7 - Add the webhook in your Story. ...
  2. Ready to use examples.
Apr 4, 2022

What is a webhook in C#? ›

WebHooks is a lightweight HTTP pattern providing a simple pub/sub model for wiring together Web APIs and SaaS services. When an event happens in a service, a notification is sent in the form of an HTTP POST request to registered subscribers.

What is a webhook example? ›

Some real-world examples of webhooks include: Automatically receive an email every morning about your first meeting in case you forget to check your calendar. Have Instagram photos upload automatically to Twitter accounts. Configure the doorbell to flash the lights when it rings.

Is webhook post or get? ›

By definition, a webhook (also called a web callback or HTTP push API) is a way for an app to provide other applications with real-time information. A webhook delivers data to other applications as it happens, meaning you get data immediately.

How do I create a custom webhook URL? ›

Creating a Postman Custom Webhook
  1. Head over to dev.flock.com and click on the Webhooks section.
  2. Since we need to post updates to Flock, we need to create an incoming webhook.
  3. Select the channel in Flock where you would like to receive the updates.
  4. Click on Save and Generate URL.
  5. Copy the Flock webhook URL.
Jun 1, 2019

How do I get a webhook URL? ›

Create a Webhook URL for a channel in Slack
  1. Navigate to your Slack workspace.
  2. Search for webhook.
  3. Click Incoming WebHooks.
  4. Click Add to Slack.
  5. For the Post to Channel list, select an existing channel or click create a new channel to create a new channel. ...
  6. Click Add Incoming WebHooks integration.

What is the purpose of webhook? ›

Webhooks typically are used to connect two different applications. When an event happens on the trigger application, it serializes data about that event and sends it to a webhook URL from the action application—the one you want to do something based on the data from the first application.

How do I send data to webhook? ›

With webhooks, it's generally a three-step process: Get the webhook URL from the application you want to send data to. Use that URL in the webhook section of the application you want to receive data from. Choose the type of events you want the application to notify you about.

What is a webhook listener? ›

A webhook listener is an app that you create that exposes an endpoint URL that will receive event messages from Connect.

What are two use cases where webhooks are effective? ›

Webhooks offer lightweight data sharing

Common use cases for webhooks include sending new email list subscriptions and unsubscriptions to a CRM system, automatically updating accounting software when invoices are paid, or setting up any type of notifications.

When should I use webhook API? ›

​​Conclusion. ​​Both APIs and webhooks have different use cases, but if your goal is to transfer data between two services, webhooks are the way to go. However, if your platform or application would demand frequent data changes, then an API should be used.

What is the difference between webhooks and WebSockets? ›

Differences between webhooks and WebSockets

Webhooks are used for one-way communication from a source application to a destination application, while WebSockets facilitate two-way communication between server and client.

How do you secure a webhook endpoint? ›

How to Secure Webhooks. 5-Step Checklist
  1. Security threats and solution recap. ...
  2. Encrypt all data. ...
  3. Verify the source. ...
  4. Verify the consumer. ...
  5. Verify the message. ...
  6. Prevent replay attacks. ...
  7. Conclusion.

Is webhooks safe? ›

Working with webhooks exposes an HTTP endpoint that can be called from any actor on your server. Without appropriate measures, this could be extremely unsafe. However, there are now well-understood strategies that ensure your webhook endpoints are secured.

How do you create a webhook token? ›

Create Webhooks
  1. Click the menu icon on the top left corner of the screen.
  2. Click Settings.
  3. Under the App Developer Tools category, click Webhooks. ...
  4. Click Create Webhook.
  5. From the Create Webhook page, enter the following information: ...
  6. Click Save.
Jul 8, 2022

What are two requirements for an application to communicate with a webhook provider? ›

  • IP address.
  • Domain Name System.
  • IP address of a Web server.

How do you create a webhook token? ›

Create Webhooks
  1. Click the menu icon on the top left corner of the screen.
  2. Click Settings.
  3. Under the App Developer Tools category, click Webhooks. ...
  4. Click Create Webhook.
  5. From the Create Webhook page, enter the following information: ...
  6. Click Save.
Jul 8, 2022

How do you create a webhook on discord? ›

How do I create a Discord webhook?
  1. Open a new browser tab and login in to your account at Discord.
  2. Click the gear icon (Edit Channel) of the channel you want to post to.
  3. Click Webhooks in the left menu.
  4. Click the Create Webhook button.
  5. Enter a Name of your choice.
  6. Click the Copy button of the Webhook URL.

How do you make a webhook say something? ›

Go to the Integrations page on your Discord server as described above and click Create Webhook. Give your webhook a descriptive name and select the channel you want your messages to be sent to. Remember to click Save Changes when you make any change to your webhook configuration.

How do you write a webhook in Python? ›

  1. Step 1: Install Flask to your Python Environment. The first step in setting up Python Webhook Integration is to install Flask to your Python environment. ...
  2. Step 2: Create a Web Service. Once you have installed Flask to your Python environment, you need to create a Web Service using Flask. ...
  3. Step 3: Run the Flask Server.
Oct 26, 2021

Videos

1. October 6, 2019 - All Linux and .NET development - Connecting to WebHooks with ASP.NET Core
(Fritz's Tech Tips and Chatter)
2. Webhooks for Beginners - Full Course
(freeCodeCamp.org)
3. C# ASP.NET Telegram Bot with Webhooks
(Jeben)
4. WebHook Tooling Demo
(brady gaster)
5. ASP.NET Community Standup - Port Tunneling for ASP.NET Core Projects
(dotNET)
6. Xero Webhooks in .NET
(Xero Developer)
Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated: 02/24/2023

Views: 5937

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.