βӔḺṪẶⱫŌŔ
βӔḺṪẶⱫŌŔ

Reputation: 1296

How to display an Exception Message for a page that uses Razor syntax

I thought that in ASP.NET Razor syntax 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?
    }
}

But it does not work.

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

Upvotes: 6

Views: 7524

Answers (1)

Steve Mallory
Steve Mallory

Reputation: 4283

When you use the <p> tag, the Razor engine drops out of C# mode and goes into HTML mode. Try forcing Razor syntax again by writing:

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

in the catch block.

Upvotes: 8

Related Questions