如何显示异常消息 (Razor/C#)
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
标记时,razor 引擎会退出 c# 模式并进入 html 模式。 尝试。
在 catch 块中
When you use the
<p>
tag, the razor engine drops out of c# mode and into html mode. Tryin the catch block.