---
title: "Use Raw String Literals to generate TwiML in C# 11"
excerpt: "Learn how Twilio uses webhooks and TwiML to give you control over how to respond to a call or text message. You can generate TwiML in many ways, and with C# 11 you can now also use Raw String Literals."
publishDate: "2023-02-15T00:00:00"
createDate: "2023-02-16T03:23:19.46"
updateDate: "2023-02-22T08:50:32.917"
category: "dotnet"
categoryName: ".NET"
tags: ["C#",".NET","ASP.NET","ASP.NET Core","Twilio","TwiML"]
permalink: "/blog/dotnet/use-raw-string-literals-to-generate-twiml-in-csharp-11"
canonical: "https://www.twilio.com/en-us/blog/use-raw-string-literals-to-generate-twiml-in-csharp11"
image: {"src":"/media/aiddtixz/py9oh53qzeh8edhp301dzk4s05noejgmod-_cncljfkrrwidth-1616.png"}
imageAlt: "Use Raw String Literals to generate TwiML in C# 11"
imageBackgroundColor: "#fff"
thumbnailImage: null
thumbnailImageAlt: "Use Raw String Literals to generate TwiML in C# 11"
metaTitle: "Use Raw String Literals to generate TwiML in C# 11"
metaDescription: "Learn how Twilio uses webhooks and TwiML to give you control over how to respond to a call or text message. You can generate TwiML in many ways, and with C# 11 you can now also use Raw String Literals."
ogTitle: "Use Raw String Literals to generate TwiML in C# 11"
ogDescription: "Learn how Twilio uses webhooks and TwiML to give you control over how to respond to a call or text message. You can generate TwiML in many ways, and with C# 11 you can now also use Raw String Literals."
ogImage: {"src":"/media/c0wjxrog/py9oh53qzeh8edhp301dzk4s05noejgmod-_cncljfkrrwidth-1616.png"}
ogImageAlt: "Use Raw String Literals to generate TwiML in C# 11"
relatedPostKeys: ["1710543d71f748eba82e9c6891718211","99831c3f1722477fa6329b6a946bd961"]
noIndex: false
noFollow: false
sitemapExclude: true
source: {"nodeId":1614,"contentKey":"c72f2fe03d50440485dc39f7816de371","sortOrder":145}
---


**This blog post was written for <a href="https://www.twilio.com/" target="_top">Twilio</a> and <a href="https://www.twilio.com/blog/use-raw-string-literals-to-generate-twiml-in-csharp11" target="_top">originally published at&nbsp;the Twilio blog</a>.**





Over the decades, C# added different ways to create a string, each with their own benefit, and C# 11 is adding another way: **Raw String Literals**. There are many use cases for Raw String Literals, and one of those use cases is to generate TwiML, but more on that later. In this tutorial, you'll learn how to generate TwiML with Raw String Literals with an ASP.NET Core Minimal API.




## Prerequisites {#prerequisites}

Here's what you will need to follow along:


-   [.NET 7 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) (later versions will work too)
-   A code editor or IDE (I recommend [JetBrains Rider](https://www.jetbrains.com/rider/), [Visual Studio 2022 Preview](https://visualstudio.microsoft.com/vs/preview/) (C# 11 is only supported in preview now, but will work in non-preview in the future.), or [VS Code](https://code.visualstudio.com/Download) with [the C# plugin](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp))
-   A free Twilio account (<a href="https://www.twilio.com/try-twilio" data-glinktype="blog post" data-glinklocation="blog:Use Raw String Literals to generate TwiML in C# 11" data-glinkname="sign up">sign up with Twilio for free</a> and get trial credit)
-   A [Twilio Phone Number](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console)
-   The [ngrok CLI](https://ngrok.com/download), and optionally, a free [ngrok account](https://dashboard.ngrok.com/signup)




<p class="panel panel--info">Experience with ASP.NET Core and Minimal APIs is not required but recommended. Here's an article that brings you up to speed on <a href="https://www.twilio.com/blog/sms-voice-dotnet-6-minimal-api">how to integrate ASP.NET Core Minimal APIs with Twilio</a>.</p>




You can find [the source code for this tutorial on GitHub](https://github.com/Swimburger/TwimlRawStringLiterals). Use it if you run into any issues, or [submit an issue](https://github.com/Swimburger/TwimlRawStringLiterals/issues), if you run into problems.


Before creating your application, let's get you up to speed on how Twilio uses webhooks and TwiML to respond to text messages and voice calls.

## Webhooks and TwiML {#webhooks-and-twiml}

Using Twilio, you can build programmatic text message and voice call applications. Twilio can do a lot of things like playing audio, gathering input, and recording a call, but Twilio doesn't know what **you** want to do in response to voice calls and text messages. That's why when Twilio receives a call or text message, Twilio will send an HTTP request to your application asking for instructions.

A [webhook](https://www.twilio.com/docs/glossary/what-is-a-webhook) is a user-defined HTTP callback. When an event happens in a service, your application is notified of that event using an HTTP request.

Twilio uses webhooks heavily throughout all its products. Here's a diagram of what it looks like when there's an incoming text message to your Twilio Phone Number and your application is handling the messaging webhook:

<p><img class="richtext-image left" src="https://assets.cdn.prod.twilio.com/images/vQXGk8VFTcIBytBwOohc9SbQH1vKWzeKNmzR3Q4Qgu26TR.width-500.png" alt="Phone texts &quot;Ahoy!&quot; to a Twilio Phone Number, Twilio sends the SMS details (from and to phone number and the body of the message) via HTTP to your web application, then your application responds with TwiML instructions instructing to respond with &quot;Hi!&quot;. Twilio receives the instructions and sends &quot;Hi!&quot; back to the original sender." width="500" height="176"></p>




When your Twilio Phone Number receives a text message, Twilio will send an HTTP request with the message details to your application. Your application then has to respond with [TwiML](https://www.twilio.com/docs/glossary/what-is-twilio-markup-language-twiml) (**Twi**lio **M**arkup **L**anguage) to instruct Twilio how to respond. TwiML is [XML](https://en.wikipedia.org/wiki/XML) with special tags defined by Twilio to provide instructions on how to respond to messages and voice calls. In the diagram depicted above, Twilio will respond with "Hi!" because the app responded with the following TwiML:




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; ">&lt;?</span><span style="color:#7f0055; font-weight:bold; ">xml</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">version</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; font-weight:bold; ">1.0</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">encoding</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">utf-8</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">  <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Message</span><span style="color:#7f0055; ">&gt;</span>Hi!<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Message</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span></pre>




You can learn more about [TwiML for Programmable Messaging here](https://www.twilio.com/docs/messaging/twiml) and [TwiML for Programmable Voice here](https://www.twilio.com/docs/voice/twiml).


Now that you know what webhooks and TwiML are, let's create a voice call application with ASP.NET Core.

## Create an ASP.NET Core Minimal API to handle voice calls {#create-an-aspnet-core-minimal-api-to-handle-voice-calls}

First, let's create an ASP.NET Core Minimal API to handle voice calls without using Raw String Literals.

Open a terminal and create a new ASP.NET Core Minimal API project using the .NET CLI:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">dotnet new web -o TwimlRawStringLiterals </span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">cd</span> TwimlRawStringLiterals</span></pre>

These commands will create the project in a folder named *TwimlRawStringLiterals* and navigate into the folder.

Open the project in your editor, and update the *Program.cs* file with the following C# code:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">using</span> System.Security;</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">var</span> builder = WebApplication.CreateBuilder(args);</span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">var</span> app = builder.Build();</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/what-does-the-fox-say</span><span style="color:#2a00ff; ">"</span>, () =&gt; Results.Text(</span>&#10;<span class="line_wrapper">    <span style="color:#2a00ff; ">@"</span><span style="color:#2a00ff; ">&lt;?xml version=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">1.0</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; "> encoding=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">utf-8</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Gather action=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; "> method=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">GET</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; "> input=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">speech</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">&gt; </span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">            &lt;Say&gt;What does the fox say?&lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">       &lt;/Gather&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Say&gt;Ring-ding-ding-ding-dingeringeding!&lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/Response&gt;</span><span style="color:#2a00ff; ">"</span>,</span>&#10;<span class="line_wrapper">    contentType: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">application/xml</span><span style="color:#2a00ff; ">"</span></span>&#10;<span class="line_wrapper">));</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">"</span>, (<span style="color:#7f0055; font-weight:bold; ">string</span> speechResult) =&gt; Results.Text(</span>&#10;<span class="line_wrapper">    <span style="color:#2a00ff; ">$@"</span><span style="color:#2a00ff; ">&lt;?xml version=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">1.0</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; "> encoding=</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">utf-8</span><span style="color:#2a00ff; ">""</span><span style="color:#2a00ff; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Say&gt;You said: </span><span style="color:#2a00ff; ">{</span><span style="color:#2a00ff; ">SecurityElement</span><span style="color:#2a00ff; ">.</span><span style="color:#2a00ff; ">Escape</span><span style="color:#2a00ff; ">(</span><span style="color:#2a00ff; ">speechResult</span><span style="color:#2a00ff; ">)</span><span style="color:#2a00ff; ">}</span><span style="color:#2a00ff; ">&lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/Response&gt;</span><span style="color:#2a00ff; ">"</span>,</span>&#10;<span class="line_wrapper">    contentType: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">application/xml</span><span style="color:#2a00ff; ">"</span></span>&#10;<span class="line_wrapper">));</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.Run();</span></pre>




When your Twilio Phone Number receives a call, Twilio will send an HTTP GET request to the <em>/what-does-the-fox-say</em> endpoint, which will set the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type">content-type</a> response header to <code>application/xml</code>, and return the following TwiML in the response:




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; ">&lt;?</span><span style="color:#7f0055; font-weight:bold; ">xml</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">version</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; font-weight:bold; ">1.0</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">encoding</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">utf-8</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; ">?&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">        <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Gather</span> action=<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">"</span> method=<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">GET</span><span style="color:#2a00ff; ">"</span> input=<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">speech</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; ">&gt;</span> </span>&#10;<span class="line_wrapper">            <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span>What does the fox say?<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">       <span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Gather</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">        <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span>Ring-ding-ding-ding-dingeringeding!<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span></pre>




Take note of how the `<Response>` node and everything in it is indented with a tab too much.


When Twilio receives the TwiML, it'll ask the caller "What does the fox say?" and listen for a speech response. When the caller responds, Twilio will send an HTTP request, this time to the */answer* endpoint, with the transcription of what the caller said. If the caller doesn't respond, Twilio will say "Ring-ding-ding-ding-dingeringeding!" to the caller.

The */answer* endpoint will retrieve the transcription from the query string parameter `SpeechResult` by binding it to the `speechResult` parameter. The `speechResult` is used to construct some more TwiML, but it is first escaped using `SecurityElement.Escape` to ensure no XML is in the variable. The */answer* endpoint will generate the following TwiML when the caller says "ring ding ding":

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; ">&lt;?</span><span style="color:#7f0055; font-weight:bold; ">xml</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">version</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; font-weight:bold; ">1.0</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; "> </span><span style="color:#7f0055; ">encoding</span><span style="color:#7f0055; ">=</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">utf-8</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span>You said: ring ding ding<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Say</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Response</span><span style="color:#7f0055; ">&gt;</span></span></pre>

When Twilio receives this TwiML, Twilio will convert "You said: ring ding ding" to speech and stream the audio to the caller.

<p class="panel panel--info">In case you're not familiar with the song I am referencing in this app, <a href="https://www.youtube.com/watch?v=jofNR_WkoCE">"What does the fox say?" originates from this famous song</a>.</p>

Start your .NET application using your editor or using the terminal using the .NET CLI:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">dotnet run</span></pre>

Before integrating Twilio, open a browser and navigate to your web application URL with the suffix */what-does-the-fox-say* to verify the TwiML that is generated.

## Make the application publicly accessible {#make-the-application-publicly-accessible}

For Twilio to be able to send HTTP requests to your local web server, the server needs to become publicly accessible. ngrok is a free secure tunneling service that can make your local web servers public.

To start ngrok, run the following ngrok command in a separate terminal:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">ngrok http <span style="color:#2a00ff; ">[</span><span style="color:#2a00ff; ">YOUR_ASPNET_URL</span>]</span></pre>




Replace <code>[YOUR_ASPNET_URL]</code> with the localhost URL from your .NET application. If you're using an HTTPS localhost URL, you'll need to <a href="https://ngrok.com/docs/secure-tunnels#tunnel-authtokens">authenticate ngrok</a>. The ngrok command will display an HTTPS <strong>Forwarding URL</strong> that makes your local web server public.




<p><img class="richtext-image left" src="https://assets.cdn.prod.twilio.com/images/apEiL_JFTXcwkjo0vLBlJd7XZjiujP63mGVlhdM-EZ7fNq.width-500.png" alt="ngrok http command output showing information about the secure tunnel, most importantly the public forwarding URLs" width="500" height="287"></p>



## Configure your Twilio Phone Number {#configure-your-twilio-phone-number}



<p>Now it's time to update your Twilio Phone Number to send HTTP requests to your <em>/what-does-the-fox-say</em> endpoint via the ngrok Forwarding URL. The URL should look something like <em><strong>https://cd2f8809cbd0.ngrok.io/what-does-the-fox-say</strong></em>.</p>




Go to the [Active Phone Numbers section](https://console.twilio.com/us1/develop/phone-numbers/manage/incoming?frameUrl=/console/phone-numbers/incoming?x-target-region=us1) in the Twilio Console and click on your Twilio Phone Number.<br>This will take you to the configuration for the phone number. Find the **Voice & Fax** section and under the "**A CALL COMES IN**" label, set the dropdown to "**Webhook**". In the text field next to it, enter your ngrok forwarding URL with */what-does-the-fox-say* appended to it. Select “**HTTP GET**” in the last dropdown. Finally, click the **Save** button at the bottom of the page.




<p><img class="richtext-image left" src="https://assets.cdn.prod.twilio.com/images/rcWjPSxeiAd8YuogrtDL7nBYmVsw1VBYSKHKI-wwCilS4x.width-500.png" alt="Voice &amp; Fax section in the Twilio Phone Number configuration page. Under the &quot;A CALL COMES IN&quot; label, a dropdown is set to &quot;Webhook&quot;, the text field next to it is configured with &quot;https://cd2f8809cbd0.ngrok.io/what-does-the-fox-say&quot;, and the dropdown next to that is set to &quot;HTTP GET&quot;." width="500" height="205"></p>



## Test that the application works {#test-that-the-application-works}

To test out your application, call your Twilio Phone Number. You should hear the question "What does the fox say?" which you can respond to, wait 5 seconds, and then your response will be read back to you.


After you've done that, stop your application using your editor, or if you're using the terminal, press `ctrl + c`.




### C# 11 and .NET 7 only: Use Raw String Literals {#c-11-and-net-7-only-use-raw-string-literals}




C# 11 is still in preview, but you can try it out today using the .NET 7 previews. Make sure you have .NET 7 installed and are using .NET 7 in your project. Then, open your project file at *TwimlRawStringLiterals.csproj* and add the `<LangVersion>preview</LangVersion>` to the first `<PropertyGroup>` node. This will enable the C# 11 preview features.


The *TwimlRawStringLiterals.csproj* file should look something like this:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Project</span> Sdk=<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Microsoft.NET.Sdk.Web</span><span style="color:#2a00ff; ">"</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">  <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">PropertyGroup</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">TargetFramework</span><span style="color:#7f0055; ">&gt;</span>net7.0<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">TargetFramework</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">Nullable</span><span style="color:#7f0055; ">&gt;</span>enable<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Nullable</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">ImplicitUsings</span><span style="color:#7f0055; ">&gt;</span>enable<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">ImplicitUsings</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; ">LangVersion</span><span style="color:#7f0055; ">&gt;</span>preview<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">LangVersion</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">  <span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">PropertyGroup</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; ">Project</span><span style="color:#7f0055; ">&gt;</span></span></pre>




Now that you have enabled C# 11, let's talk about the new <a href="https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/">Raw String Literals</a>. To use Raw String Literals, you start and end your string with at least 3 double quotes. For example:




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>"</span>&#10;<span class="line_wrapper">    I like using Raw String Literals,</span>&#10;<span class="line_wrapper">    because it supports multi-line text,</span>&#10;<span class="line_wrapper">    you don<span style="color:#2a00ff; ">'</span>t need to escape forward slashes (/), backslashes (\), and double quotes* ("),</span>&#10;<span class="line_wrapper">    and you can indent your code better without having the indentation included in your string. </span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>"</span></pre>



<p class="panel panel--warning">The syntax highlighting may be off in the next snippets and snippet above because the blog doesn't have C# 11 highlighting yet.</p>

You don't have to escape forward slashes, backslashes, and you also don't have to escape your double quotes, more or less. You can use double quotes as long as the number of subsequent double quotes is less than the amount of double quotes you started and ended your string with. This is clearer with an example:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">// 3 start and end double quotes, so 1 and 2 subsequent double quotes is allowed</span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">This </span><span style="color:#2a00ff; ">"</span> and this <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span> is allowed.<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>" </span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">// 3 start and end double quotes, so 3 subsequent double quotes is NOT allowed</span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">This </span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span> is not allowed.<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>"</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">// 4 start and end double quotes, so 3 subsequent double quotes is NOT allowed</span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>This <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "> is allowed.</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>"</span></pre>

Line one and two are valid Raw String Literals because the number of subsequent double quotes is lower than the number of double quotes used to start and end the Raw String Literal.


Raw String Literals also makes it easier to format your strings. Take the following method that generates a Verbatim String (`@`) as an example:




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> GenerateHtml()</span>&#10;<span class="line_wrapper">{</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">return</span> <span style="color:#2a00ff; ">@"</span><span style="color:#2a00ff; "></span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;p&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    This is a &lt;b&gt;multi-line Verbatim String&lt;/b&gt; to generate some HTML</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/p&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    </span><span style="color:#2a00ff; ">"</span>;</span>&#10;<span class="line_wrapper">}</span></pre>

The resulting string will be:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"></span>&#10;<span class="line_wrapper">        <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; font-weight:bold; ">p</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">        This is a <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; font-weight:bold; ">b</span><span style="color:#7f0055; ">&gt;</span>multi-line Verbatim String<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; font-weight:bold; ">b</span><span style="color:#7f0055; ">&gt;</span> to generate some HTML</span>&#10;<span class="line_wrapper">        <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; font-weight:bold; ">p</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper"></span></pre>

Notice the newlines and the tab indentation? I formatted the Verbatim String for the sake of readability, but as a result I also got the newlines and tabs in it which I do not want.

Let's take a look at the Raw String Literal version:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">string</span> GenerateString()</span>&#10;<span class="line_wrapper">{</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">return</span> <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "></span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;p&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    This is a &lt;b&gt;multi-line Raw String Literal&lt;/b&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/p&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    </span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>;</span>&#10;<span class="line_wrapper">}</span></pre>

The resulting string now looks like this:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; font-weight:bold; ">p</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper">This is a <span style="color:#7f0055; ">&lt;</span><span style="color:#7f0055; font-weight:bold; ">b</span><span style="color:#7f0055; ">&gt;</span>multi-line Raw String Literal<span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; font-weight:bold; ">b</span><span style="color:#7f0055; ">&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; ">&lt;/</span><span style="color:#7f0055; font-weight:bold; ">p</span><span style="color:#7f0055; ">&gt;</span></span></pre>

The unnecessary newlines and tabs are gone. The way Raw String Literals indent your strings depends on where you place the starting and ending double quotes, and also how you indent the content of your string. It's a little funky and hard to explain all the variations, so I recommend experimenting with it and reading [the Microsoft documentation on Raw String Literals](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals) to learn more.

Using this knowledge, you can apply Raw String Literals to your endpoints like this:

<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/what-does-the-fox-say</span><span style="color:#2a00ff; ">"</span>, () =&gt; Results.Text(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "></span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;?xml version=</span><span style="color:#2a00ff; ">"</span>1.0<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "> encoding=</span><span style="color:#2a00ff; ">"</span>utf-8<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Gather action=</span><span style="color:#2a00ff; ">"</span>/answer<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "> method=</span><span style="color:#2a00ff; ">"</span>GET<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "> input=</span><span style="color:#2a00ff; ">"</span>speech<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">            &lt;Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">                What does the fox say?</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">            &lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;/Gather&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Say&gt;Ring-ding-ding-ding-dingeringeding!&lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    </span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span>,</span>&#10;<span class="line_wrapper">    contentType: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">application/xml</span><span style="color:#2a00ff; ">"</span></span>&#10;<span class="line_wrapper">));</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">"</span>, (<span style="color:#7f0055; font-weight:bold; ">string</span> speechResult) =&gt; Results.Text(<span style="color:#2a00ff; ">$"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "></span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;?xml version=</span><span style="color:#2a00ff; ">"</span>1.0<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; "> encoding=</span><span style="color:#2a00ff; ">"</span>utf-8<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">?&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">        &lt;Say&gt;You said: {SecurityElement.Escape(speechResult)}&lt;/Say&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    &lt;/Response&gt;</span></span>&#10;<span class="line_wrapper"><span style="color:#2a00ff; ">    </span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">"</span> ,</span>&#10;<span class="line_wrapper">    contentType: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">application/xml</span><span style="color:#2a00ff; ">"</span></span>&#10;<span class="line_wrapper">));</span></pre>

Now the resulting TwiML will be readable and not contain unnecessary newlines and tabs.

### Other ways to generate TwiML {#other-ways-to-generate-twiml}

There are many other ways you could generate TwiML. Since TwiML is XML, you can use the APIs from the `System.Xml` and `System.Xml.Linq` namespace. Twilio also has a helper library for .NET that lets you generate TwiML in an object-oriented way, and the helper library for ASP.NET helps you return the TwiML in the HTTP response.


In the terminal where you ran your project, run the following commands to add the [Twilio package](https://www.nuget.org/packages/Twilio) and [Twilio.AspNet.Core package](https://www.nuget.org/packages/Twilio):




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper">dotnet add package Twilio</span>&#10;<span class="line_wrapper">dotnet add package Twilio<span style="color:#7f0055; font-weight:bold; ">.</span>AspNet<span style="color:#7f0055; font-weight:bold; ">.</span>Core</span></pre>




Then, update the <em>Program.cs</em> file with the following code:




<pre class="code_syntax" style="color:#000000;background:#ffffff;"><span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">using</span> Twilio.AspNet.Core.MinimalApi;</span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">using</span> Twilio.TwiML;</span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">using</span> Twilio.TwiML.Voice;</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">var</span> builder = WebApplication.CreateBuilder(args);</span>&#10;<span class="line_wrapper"><span style="color:#7f0055; font-weight:bold; ">var</span> app = builder.Build();</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/what-does-the-fox-say</span><span style="color:#2a00ff; ">"</span>, () =&gt;</span>&#10;<span class="line_wrapper">{</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">var</span> response = <span style="color:#7f0055; font-weight:bold; ">new</span> VoiceResponse();</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">var</span> gather = <span style="color:#7f0055; font-weight:bold; ">new</span> Gather(</span>&#10;<span class="line_wrapper">        input: <span style="color:#7f0055; font-weight:bold; ">new</span> List&lt;Gather.InputEnum&gt; {Gather.InputEnum.Speech},</span>&#10;<span class="line_wrapper">        action: <span style="color:#7f0055; font-weight:bold; ">new</span> Uri(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">"</span>, UriKind.Relative),</span>&#10;<span class="line_wrapper">        method: Twilio.Http.HttpMethod.Get</span>&#10;<span class="line_wrapper">    );</span>&#10;<span class="line_wrapper">    gather.Append(<span style="color:#7f0055; font-weight:bold; ">new</span> Say(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">What does the fox say?</span><span style="color:#2a00ff; ">"</span>));</span>&#10;<span class="line_wrapper">    response.Append(gather);</span>&#10;<span class="line_wrapper">    response.Say(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Ring-ding-ding-ding-dingeringeding!</span><span style="color:#2a00ff; ">"</span>);</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">return</span> Results.Extensions.TwiML(response);</span>&#10;<span class="line_wrapper">});</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.MapGet(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">/answer</span><span style="color:#2a00ff; ">"</span>, (<span style="color:#7f0055; font-weight:bold; ">string</span> speechResult) =&gt;</span>&#10;<span class="line_wrapper">{</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">var</span> response = <span style="color:#7f0055; font-weight:bold; ">new</span> VoiceResponse()</span>&#10;<span class="line_wrapper">        .Say(<span style="color:#2a00ff; ">$"</span><span style="color:#2a00ff; ">You said: </span><span style="color:#2a00ff; ">{</span><span style="color:#2a00ff; ">speechResult</span><span style="color:#2a00ff; ">}</span><span style="color:#2a00ff; ">"</span>);</span>&#10;<span class="line_wrapper">    <span style="color:#7f0055; font-weight:bold; ">return</span> Results.Extensions.TwiML(response);</span>&#10;<span class="line_wrapper">});</span>&#10;<span class="line_wrapper"></span>&#10;<span class="line_wrapper">app.Run();</span></pre>

The resulting TwiML will be the same as before. Notice, however, that you don't need to use the `SecurityElement.Escape` anymore to escape the user input, because the Twilio helper library will XML encode it for you.<br>Under the hood, the helper library uses the APIs from `System.Linq.Xml` to generate the XML string which will also do the formatting for you.

Now, you may be wondering, why would you use strings when you can use the Twilio helper library, or maybe you're wondering why you would use the helper library when you can use strings.


The advantages of creating TwiML using strings, is that you don't need any dependencies and it's more memory efficient & performant. However, it is easier to make a mistake in your TwiML and there will be no compilation errors to prevent you from doing that. You also have to XML escape user input yourself to protect yourself from XML injection.


The advantages of using the Twilio helper library are that you are using fully typed objects and methods that provide [IntelliSense](https://en.wikipedia.org/wiki/Intelligent_code_completion) in IDEs. If you make a typo, your project will not build and you will receive a compiler error telling you where your typo is. However, you do depend on the Twilio helper library and its dependencies, and you have to create a bunch of objects that are ultimately serialized to an XML string which is less memory efficient and slower.

They both have their own advantages and disadvantages, but you can mix and match based on your use case.

## Next steps {#next-steps}

You learned how Twilio uses webhooks and TwiML to give you control over how to respond to a call or text message. You then learned how to generate TwiML instructions using C# 11's new Raw String Literal feature, and compared it to generating TwiML with the Twilio helper library.

Here are a couple more resources to further your learning on Minimal APIs and Twilio:


-   [Forward Voicemails with Transcript to your Email using C# and ASP.NET Core](https://www.twilio.com/blog/forward-voicemails-with-transcript-to-your-email-using-csharp-and-aspnetcore)
-   [Find your U.S. Representatives and Congressional Districts with SMS and ASP.NET Core](https://www.twilio.com/blog/find-us-representatives-congressional-districts-with-sms-aspnetcore)
-   [How to test ASP.NET Core Minimal APIs](https://www.twilio.com/blog/test-aspnetcore-minimal-apis)


We can't wait to see what you build. Let us know!
