[英]Reponse.Redirect doesn't work immediately C#
我有以下C#代碼:
string ProfileID, UserID;
try
{
ProfileID = Request.QueryString["ProfileID"].ToString();
string SelectQuery;
DataSet ds;
try
{
UserID = Session["UserID"].ToString();
if (ProfileID == UserID)
{
Response.Redirect("user/Default.aspx", true);
return;
}
else
{
//some code here
}
}
finally
{
//some code here
}
}
catch {
Response.Redirect("DoesNotExists.aspx");
}
這里的問題是,如果條件為真( if (ProfileID == UserID)
),則代碼cotinue在此行中運行而不是重定向: Response.Redirect("user/Default.aspx", true);
redicet在這一行: Response.Redirect("DoesNotExists.aspx");
我的問題是如何解決它。
希望得到幫助,謝謝!
您無法在Try-Catch
重定向,因為拋出了ThreadAbortedException
。 您需要將false
傳遞給Response.Redirect
才能執行此操作。
Boolean表示當前頁面的執行是否應該終止。
因此,要么保存重定向位置,要么在try-catch之后重定向,要么使用Response.Redirect(url, false)
。
string ProfileID, UserID, redirectUrl;
try
{
ProfileID = Request.QueryString["ProfileID"].ToString();
string SelectQuery;
DataSet ds;
try
{
UserID = Session["UserID"].ToString();
if (ProfileID == UserID)
{
redirectUrl = "user/Default.aspx";
}
else
{
//some code here
}
}
finally
{
//some code here
}
}
catch
{
redirectUrl = "DoesNotExists.aspx";
}
if(!string.IsNullOrEmpty(redirectUrl))
Response.Redirect(redirectUrl);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.