我有以下代码:
protected Dictionary<NpInfoHelper, object> Informer;
protected override void OnInit(EventArgs e)
{
if (Session.Keys.Count == 1)
{
Session.Abandon();
Response.RedirectPermanent("~/Pages/Login?e=true", true);
}
else
{
Informer = (Dictionary<NpInfoHelper, object>)Session["Informer"];
}
base.OnInit(e);
}在每个后端.cs文件的.aspx文件中。
我觉得这是重复的。请注意,我将在每个Informer文件中使用.aspx.cs。
以上代码重复了19次。我能做一个吗?
发布于 2014-04-27 05:22:03
根据注释,您可能需要的是一个抽象类MyAppPage:
abstract class MyAppPage : Page {
protected Dictionary<NpInfoHelper, object> Informer;
protected override void OnInit(EventArgs e)
{
if (Session.Keys.Count == 1)
{
Session.Abandon();
Response.Redirect("~/Pages/Login?e=true", true);
}
else
{
Informer = (Dictionary<NpInfoHelper, object>)Session["Informer"];
}
base.OnInit(e);
}
}然后您的其他页面将扩展MyAppPage而不是Page。
最后,您将注意到我冒昧地将Response.RedirectPermanent更改为Response.Redirect。永久重定向返回301响应“永久移动”,并表示当前页面不再存在于此位置,并已移到其他位置。使用它来重定向没有登录的人可能会让爬虫们感到困惑。
https://codereview.stackexchange.com/questions/48298
复制相似问题