C# 我如何让 Response.Redirect() 从 MasterPage 工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/464948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i have Response.Redirect() work from MasterPage?
提问by p4bl0
I have a problem: when i call a Response.Redirect() from the MasterPage it doesn't work. Well, debugging i can see that until the Pre_Render() method the target page is loaded, but then is rendered the previous page.
我有一个问题:当我从 MasterPage 调用 Response.Redirect() 时它不起作用。好吧,调试我可以看到,直到 Pre_Render() 方法加载目标页面,然后呈现上一页。
Here's some code to better explain:
这里有一些代码可以更好地解释:
(from MasterPageMain.master.cs)
(来自 MasterPageMain.master.cs)
protected void Page_Init(object sender, EventArgs e)
{
string m_QueryStringValue = Request.QueryString.Get("action");
if ((!string.IsNullOrEmpty(m_QueryStringValue)) && (m_QueryStringValue.ToLower() == "send"))
{
if (Session["to"] != null && Session["to"] is List<string>) this.SendPageByMail();
else
{
Session.Add("AddressToSend", Request.RawUrl);
Response.Redirect("~/chooseRecipients.aspx");
}
}
}
I have a javascript that adds the querystring adding "action=send" when i click on the Send button.
我有一个 javascript,当我单击“发送”按钮时,它会添加查询字符串并添加“action=send”。
If i am on page "~/somethingInterestingToSend()" -for example- i want to get on the recipient selection page, but when i click the Send button i see always the same page.
如果我在页面“~/somethingInterestingToSend()”——例如——我想进入收件人选择页面,但是当我点击发送按钮时,我总是看到相同的页面。
What coul be the mistake?
可能是什么错误?
回答by Echostorm
I don't know if its the root of your problem but I'd change 2 things. I'd change your code to:
我不知道这是否是您问题的根源,但我会改变两件事。我会将您的代码更改为:
Response.Redirect("~/chooseRecipients.aspx", false);
and move the logic to PageLoad
并将逻辑移至 PageLoad
回答by HectorMac
I am not sure I fully understand your description of the problem, but here are a few things to consider:
我不确定我是否完全理解您对问题的描述,但需要考虑以下几点:
You mention a send button. If this is an , clicking it fires a javascript postback to the server. This postback is to the original URL. I'm not sure what you are modifying with Javascript, but I don't think it would change the postback URL (and querystring).
你提到了一个发送按钮。如果这是 ,单击它会向服务器发送 javascript 回发。此回发指向原始 URL。我不确定您正在使用 Javascript 修改什么,但我认为它不会更改回发 URL(和查询字符串)。
If you need to perform logic to redirect you might want to execute in the button click event on the server.
如果您需要执行重定向逻辑,您可能希望在服务器上的按钮单击事件中执行。
If you don't need to execute any logic on the server, you could to the redirect with javascript:
如果您不需要在服务器上执行任何逻辑,您可以使用 javascript 重定向:
window.location = "chooseRecipients.aspx";
回答by JerKimball
Can't test this theory (running from memory at the moment), but give this a shot:
无法测试这个理论(目前从记忆中运行),但试一试:
(sorry, cleaned up the code a bit as well)
(抱歉,也稍微整理了一下代码)
protected void Page_Init(object sender, EventArgs e)
{
string m_QueryStringValue = Request.QueryString.Get("action") ?? "";
if (m_QueryStringValue.ToLower() == "send")
{
if ( (Session["to"] as List<string>) != null)
{
this.SendPageByMail();
}
else
{
Session.Add("AddressToSend", Request.RawUrl);
Response.Redirect("~/chooseRecipients.aspx", false);
HttpContext.Current.ApplicationInstance.CompleteRequest()
}
}
}
回答by MasterPitch
If you want to redirect not logged in users to a login-page you should check the Request.RawUrl() like this:
如果您想将未登录的用户重定向到登录页面,您应该像这样检查 Request.RawUrl():
string strURL=Request.RawUrl.ToUpper();
if (!strURL.Contains("LOGIN.ASPX") && !strURL.Contains("LOGOUT.ASPX")
&& !strURL.Contains("ERROR.ASPX") && !strURL.Contains("UNDERCONSTRUCTION.ASPX"))
{
Response.Redirect("~/Login.aspx", false);
}
All other sites will be redirected.
所有其他站点都将被重定向。