我具有以下用于设置“个人资料”页面的代码
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;
}
}
catch
{
throw new ArgumentException("Couldn't retreive");
}
finally
{
ProfileHelper.DecrementActiveRequests();
}
}
}
哪个设置-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="profile.aspx.cs" Inherits="profile" %>
<!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>
有没有办法可以捕获此错误?我尝试添加一个try catch函数,但这似乎并没有解决问题。
我的getProfile方法如下所示-
public static Profile GetProfile(string username)
{
// We cache the profile for 1 minute so we can speed up access, but still return fairly fresh profiles
// lock for thread safety
Profile profile = null;
lock (s_profileCache)
{
if (!s_profileCache.TryGetValue(username, out profile)
|| profile.CreateTime < DateTime.UtcNow.AddMinutes(-1))
{
// missing or stale, fetch it
profile = RetrieveProfile(username);
if(profile != null)
{
// save it in the cache
s_profileCache[username] = profile;
// Store purchases in purchase cache, if not already present (puchases don't change
// so probably no point overwriting them?)
foreach (var p in profile.Purchases)
{
Purchase purchase = null;
s_purchaseCache[p.Id] = purchase;
}
}
}
}
return profile;
}