I want to check for Session objects and if none exist, redirect the user to index.html.
我想检查Session对象,如果不存在,则将用户重定向到index.html。
Regarding the two pieces of code below, I was wondering when should I use one over the other?
关于下面的两段代码,我想知道我应该何时使用其中一个?
I think I should use version 1 all the time but I'm unsure.
我想我应该一直使用版本1,但我不确定。
version 1:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
}
else
{
//code
}
}
}
version 2:
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
}
else if (!Page.IsPostBack)
{
//code
}
}
EDIT: Thank you for the replies. I think the two conditions shouldn't be linked and two if statements would be most appropriate (in my situation).
编辑:谢谢你的回复。我认为这两个条件不应该联系起来,两个if语句最合适(在我的情况下)。
3 个解决方案
#1
1
Well, it depends. There is a minor difference in logic there.
这得看情况。那里的逻辑存在细微差别。
Version 1 will redirect only if it's not a Postback
action, i.e. when a user is not submitting a form.
仅当版本1不是回发操作时,即当用户未提交表单时,版本1才会重定向。
Version 2 will redirect if a user submits a form or requests a page.
如果用户提交表单或请求页面,则版本2将重定向。
I would go for option 3:
我会选择选项3:
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
return; //return here so this logic isn't mixed with the postback logic.
}
if (!Page.IsPostBack)
{
//code
}
}
#2
2
Essentially your session check and postback check are parts of two different workflows, so to say. So chaining them in one if-else
block might only confuse future code reader. I would suggest third version:
基本上你的会话检查和回发检查是两个不同工作流程的一部分,所以说。因此,将它们链接在一个if-else块中可能只会混淆未来的代码阅读器。我建议第三版:
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
return;
}
if (!Page.IsPostBack)
{
//code
}
}
Here two checks are separated, and code flow appears to be more transparent.
这里分开了两个检查,代码流似乎更透明。
#3
2
It really depends on what you are trying to achieve. Version 1 will never redirect on a postback, even if Session.Count == 0. Without knowing what you want to accomplish it is impossible to tell you which one to use.
这实际上取决于你想要达到的目标。版本1永远不会重定向回发,即使Session.Count == 0.不知道你想要完成什么,也不可能告诉你使用哪一个。
#1
1
Well, it depends. There is a minor difference in logic there.
这得看情况。那里的逻辑存在细微差别。
Version 1 will redirect only if it's not a Postback
action, i.e. when a user is not submitting a form.
仅当版本1不是回发操作时,即当用户未提交表单时,版本1才会重定向。
Version 2 will redirect if a user submits a form or requests a page.
如果用户提交表单或请求页面,则版本2将重定向。
I would go for option 3:
我会选择选项3:
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
return; //return here so this logic isn't mixed with the postback logic.
}
if (!Page.IsPostBack)
{
//code
}
}
#2
2
Essentially your session check and postback check are parts of two different workflows, so to say. So chaining them in one if-else
block might only confuse future code reader. I would suggest third version:
基本上你的会话检查和回发检查是两个不同工作流程的一部分,所以说。因此,将它们链接在一个if-else块中可能只会混淆未来的代码阅读器。我建议第三版:
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect("~/index.html");
return;
}
if (!Page.IsPostBack)
{
//code
}
}
Here two checks are separated, and code flow appears to be more transparent.
这里分开了两个检查,代码流似乎更透明。
#3
2
It really depends on what you are trying to achieve. Version 1 will never redirect on a postback, even if Session.Count == 0. Without knowing what you want to accomplish it is impossible to tell you which one to use.
这实际上取决于你想要达到的目标。版本1永远不会重定向回发,即使Session.Count == 0.不知道你想要完成什么,也不可能告诉你使用哪一个。