为什么要点两次button以后Session值才有效?
//btn
private void Button1_Click(object sender, System.EventArgs e)
{
Session[ "UserName "]= "abc ";
Page.Response.Redirect( "http://localhost/test/welcome.aspx);
}
//httpmodule
namespace test
{
public class UserAuthorizationModule : IHttpModule
{
#region IHttpModule 成员
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (application.Context.Session[ "UserName "] == null || application.Context.Session[ "UserName "].ToString().Trim() == " ")
{
string requestUrl = application.Request.Url.ToString();
string requestPage = requestUrl.Substring(requestUrl.LastIndexOf( '/ ') + 1);
if (requestPage != "login.aspx ")
application.Server.Transfer( "login.aspx ");
}
}
public void Dispose()
{ }
#endregion
}
调试时
在btn的事件里Session已经有值了,HttpContext.Session= "abc ",
但进httpmodule时HttpContext.Session就为空了
然后再点button以后进httpmodule时HttpContext.Session才是 "abc "
之后输地址加login.aspx再点btn的话也正常,就是第一次点btn时Session的值在httpmodule里为空,不知道为什么?
[解决办法]
用相对地址,别用绝对地址