首页 > 解决方案 > 配置文件页面抛出 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 错误。有没有办法可以捕捉到这个错误?

标签: c#.net

解决方案


主要问题是获取错误描述。我假设您在生产环境中,因为通常在开发中错误会清楚地显示。您可以覆盖页面的 OnError 方法以获取确切的错误。处理 page_error 事件也是一种可能的方法。否则你可以在 global.asax 的 Application_error 上得到它


推荐阅读