如何显示异常消息 (Razor/C#)

发布于 2024-11-09 19:58:30 字数 558 浏览 5 评论 0原文

我有 C# 背景,使用桌面应用程序,主要是用于 Web 内容的 PHP,我认为使用 Razor 代码,您可以执行类似的操作来显示异常消息(就像在桌面应用程序中一样):

@{
    // 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?
    }
}

注意:我有意将那里有两个@来强制异常,所以我可以看到如何显示异常消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-11-16 19:58:30

当您使用

标记时,razor 引擎会退出 c# 模式并进入 html 模式。 尝试。

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

在 catch 块中

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文