Substitution取Session的值的时候,一般都是弹出异常Session未初始化,狂晕,网上搜索资料很少
Code
前始:
<asp:Substitution runat="Server" ID="SubstitutionLogin" MethodName="SetLoginStatus" />
后台:
public static string SetLoginStatus(HttpContext context)
{
string reStr = "";
try
{
if (context.Session["User_Id"] == null)
{
reStr = "<ul class=\"hello\"><li>您好,欢迎来到试客联盟!</li><li><a href=\"Login.aspx?URL=" + context.Request.Url.ToString() + "\">[ 请登陆 ]</a></li><li><a href=\"Register2.aspx\">[ 免费注册 ]</a></li><li><a href=\"Register2.aspx?type=1\">[ 企业会员注册 ]</a></li></ul>";
}
else
{
if (context.Session["user"] != null)
{
Shikee.DBUtility.Base.User user = context.Session["user"] as Shikee.DBUtility.Base.User;
reStr = "<ul class=\"hello\"><li>您好,{0},欢迎来到试客联盟!</li><li>[<a href='{1}' > 后台管理</a>] </li><li>[<a href='LogOut.aspx' >退出</a>]</li></ul>";
if (((Shikee.DBUtility.Base.User)context.Session["user"]).Usertype_id == 1)
{
reStr = String.Format(reStr, ((Shikee.DBUtility.Base.User)context.Session["user"]).User_nickname, "/UserMember/UserPerson/index.aspx");
}
else
{
reStr = String.Format(reStr, ((Shikee.DBUtility.Base.User)context.Session["user"]).User_nickname, "/UserMember/UserCompany/index.aspx");
}
}
}
}
catch
{
reStr = "<ul class=\"hello\"><li>您好,欢迎来到试客联盟!</li><li><a href=\"Login.aspx?URL=" + context.Request.Url.ToString() + "\">[ 请登陆 ]</a></li><li><a href=\"Register2.aspx\">[ 免费注册 ]</a></li><li><a href=\"Register2.aspx?type=1\">[ 企业会员注册 ]</a></li></ul>";
}
return reStr;
}
最后的结果是,用户怎么登陆,状态还是未登陆,十分火急!