Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

SMS and MMS Notifications with C# and ASP.NET MVC

Need to hail your server administrators when something goes wrong with your servers? We've got you covered - today we'll cover adding notifications on server exceptions to your C# ASP.NET MVC application. We'll cover all the key plumbing to make it happen!

See how EMC uses Twilio SMS to send IT alerts to 68,000 employees.

Why wait? Click the button below to move to the next step of the tutorial.

Let's Notify the Administrators!

List The Server Administrators - and Whomever Else to Contact

ここでは、サーバーエラーが発生した場合に通知する管理者のリストを作成します。唯一必要なデータは、各管理者の PhoneNumber です。

        
        
        
        
        ServerNotifications.Web/App_Data/administrators.csv

        CSV list of phone numbers to notify

        ServerNotifications.Web/App_Data/administrators.csv

        With this list in hand, let's configure the Twilio REST client and see how we use it to send a notification on application exceptions.

        How Do We Configure the Client?

        Configure the Twilio C# REST Client

        To send a message, we'll need to initialize the Twilio C# Helper Library. This requires reading our TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN defined at ServerNotifications.Web/Web.config

              
              
              
              
              ServerNotifications.Web/Domain/Twilio/RestClient.cs

              Twilio Client Wrapper

              ServerNotifications.Web/Domain/Twilio/RestClient.cs

              Now that we have our Twilio Client setup, let's see how to handle exceptions.

              You're Exceptional

              Handle All C# Application Exceptions

              In an ASP.NET MVC application, we can handle errors at the application level. Note that any other exceptions caught in the code (using your own try/catch blocks) are not being captured and processed here.

                    
                    
                    
                    
                    ServerNotifications.Web/Global.asax.cs

                    Catch any uncaught exception raised in the application

                    ServerNotifications.Web/Global.asax.cs

                    Any uncaught exception will be handled here. Let's see how we can use this exception to hail our administrator list.

                    Creating a Custom Alert Message

                    Here we create an alert message to send out via text message. You might also decide to include a picture with your alert message... maybe a screenshot of the application when the crash happened? A meme to calm everyone down?

                          
                          
                          
                          
                          ServerNotifications.Web/Global.asax.cs

                          The error handling is setup to capture application wide errors and forward them to all the lucky administrators.

                          Let’s take a closer look at how the Notifier forwards these errors to the administrators.

                          Getting the Administrator List Into the Application

                          Read the Administrators from the CSV File

                          With CSVReader, we can open and parse our list of administrators from our CSV file. We use the method GetRecords to read all the records.

                                
                                
                                
                                
                                ServerNotifications.Web/Models/Repository/AdministratorsRepository.cs

                                Open and parse our administrator list

                                ServerNotifications.Web/Models/Repository/AdministratorsRepository.cs

                                We've seen how to load a list of administrators from a file. Now let's see how to send a message to each of them.

                                Send a Text Message Blast

                                There are the three parameters needed to send an SMS using the Twilio REST API: From, To, and Body.

                                US and Canadian phone numbers can also send an image with the message. (Other countries can as well, but a shortened URL pointing to the image will be appended).

                                      
                                      
                                      
                                      
                                      ServerNotifications.Web/Domain/Twilio/RestClient.cs

                                      Uses the Twilio Client to send a message

                                      ServerNotifications.Web/Domain/Twilio/RestClient.cs

                                      That's it! We've just implemented an automated server notification system that can send out C# ASP.NET MVC server alerts if anything goes wrong.

                                      Let's see some other fine features you can add to your application, next.

                                      他に構築できるもの

                                      次はどこでしょうか?

                                      Twilio and C# are great together! Here are two more ideas for useful features:

                                      二要素認証

                                      パスワードに加えてユーザーの携帯電話を検証することで、ログインシステムのセキュリティーを強化します。

                                      アポイントメント リマインダー

                                      近く予定されているアポイントメントがあったときに、お客様にテキストメッセージを送信します。このチュートリアルでは、バックグラウンドジョブからそれを行う方法を示します。

                                      これは役に立ちましたか?

                                      Thanks for checking out this tutorial! Tweet us @twilio to let us know what you've built... and what you're building.

                                      Agustin Camino Jose Oliveros Kat King Hector Ortega Samuel Mendes Andrew Baker Paul Kamp
                                      Rate this page:

                                      ヘルプが必要ですか?

                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。