我有一个CustomRequestContext
对象,必须在每次请求后处理。我在Page_Load
创建并在Page_Unload
中处理它。唯一的问题是在某些情况下我需要调用Server.Transfer
来重定向到另一个aspx页面。在这种情况下,在准备卸载新页面之前,不应卸载该对象。实现这个目标最好的方法是什么?
答案 0 :(得分:0)
为所有asp.net页面创建一个自定义PageBase类,如下所述,并在Page_Load
和Page_Unload
事件上公开CustomRequestContext。
/// <summary>
/// Base of front end web pages.
/// </summary>
public class PageBase : System.Web.UI.Page
{
/// <summary>
/// Initializes a new instance of the Page class.
/// </summary>
public Page()
{
this.Load += new EventHandler(this.Page_Load);
this.UnLoad += new EventHandler(this.Page_UnLoad);
}
/// <summary>
/// Page Load
/// </summary>
/// <param name="sender">sender as object</param>
/// <param name="e">Event arguments</param>
private void Page_Load(object sender, EventArgs e)
{
try
{
//Dispose the object here, assuming it is IDisposable.
//You can apply your own Disposition steps here..
CustomRequestContext.Dispose();
}
catch
{
//handle the situation gracefully here.
}
}
/// <summary>
/// Page UnLoad
/// </summary>
/// <param name="sender">sender as object</param>
/// <param name="e">Event arguments</param>
private void Page_UnLoad(object sender, EventArgs e)
{
try
{
//Dispose the object here, assuming it is IDisposable.
//You can apply your own Disposition steps here..
CustomRequestContext.Dispose();
}
catch
{
//handle the situation gracefully here.
}
}
}
答案 1 :(得分:0)
我通过允许对象一次拥有一个页面来解决这个问题。如果我想放弃对页面的控制,我会将它设置为null,然后它不会在页面的析构函数中释放。