c# - 配置文件页面抛出 HTTP 500 错误
问题描述
我正在开发我的 .Net 应用程序,以下页面为用户的个人资料生成一个页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Profile for user: <%= _profile.Username %></h1>
<h2>Purchases</h2>
<% foreach (var purchase in _purchases) { %>
<h3><%= purchase.Id %>: <%= purchase.Quantity %> items at $<%= Math.Round(purchase.Price / 100.0) %>.</h3>
<% } %>
</div>
</form>
</body>
</html>
这是它背后的 CS 代码 -
public partial class profile : System.Web.UI.Page
{
protected Profile _profile;
protected List<Purchase> _purchases;
protected void Page_Load(object sender, EventArgs e)
{
ProfileHelper.IncrementActiveRequests();
try
{
var username = Context.Request.QueryString["username"];
if (String.IsNullOrEmpty(username))
{
throw new ArgumentException("Missing required parameter: username");
}
else
{
_profile = ProfileHelper.GetProfile(username);
// Extract the purchases
_purchases = _profile.Purchases;
}
}
finally
{
ProfileHelper.DecrementActiveRequests();
}
}
}
我遇到的问题是页面不断从 GET 用户名请求中抛出 HTTP 500 错误。有没有办法可以捕捉到这个错误?
解决方案
主要问题是获取错误描述。我假设您在生产环境中,因为通常在开发中错误会清楚地显示。您可以覆盖页面的 OnError 方法以获取确切的错误。处理 page_error 事件也是一种可能的方法。否则你可以在 global.asax 的 Application_error 上得到它
推荐阅读
- c - Windows 中的 C 运行时库的历史
- tinymce - 项目符号和编号在软输入上不起作用,并且在以前的版本中工作
- java - 排行榜类问题
- react-native - 网络请求在反应本机博览会客户端中失败
- javascript - 在 React 项目中安装语义 UI 时遇到很多麻烦
- excel - 在“PROD”和“DEV”模式之间快速切换
- hybris - Hybris 启动错误
- java - PDFBox 2.0.15 的外部签名
- elasticsearch - 日期范围内的 Elasticsearch 聚合
- php - 如何通过 ajax 从 Summernote 编辑器将数据发送到 php 文件?