6

I come from a C# background with Desktop apps and mostly PHP for web stuff, and I thought that with Razor code you could do something like this to display an exception message (just like you can in desktop apps):

@{
    // Other code....



    try
    {
         WebMail.Send(to: "talk@@blah.com",  
        subject: "New message from - " + email, 
        body: message 
        ); 

        <p>Thanks for your message. We'll be in touch shortly!</p>
    }
    catch(Exception exception)
    {
          <p>exception.Message;</p> // Why doesn't this display exception details?
    }
}

Note: I have intentionally put two @'s in there to force an exception, so I can see how to display exception messages.

βӔḺṪẶⱫŌŔ
  • 1,276
  • 3
  • 17
  • 33
  • Not part of the issue, but you shouldn't be escaping @ signs in a string: talk@@blah.com should be talk@blah.com – Mike Brind May 26 '11 at 04:19
  • That was the whole point. I added two @'s to *force* an exception, so I could test the results of the catch clause, to see how I should write exception code. – βӔḺṪẶⱫŌŔ May 26 '11 at 13:23
  • 1
    Take a look at the following article: [http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax](http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax) The "Handling Errors" section gives some ideas on how it should be done. Hope this helps. – Kamyar May 25 '11 at 16:35

1 Answers1

8

When you use the <p> tag, the razor engine drops out of c# mode and into html mode. Try

<p>@exception.Message;</p>

in the catch block.

Steve Mallory
  • 4,245
  • 1
  • 28
  • 31