C# 对于某些无效请求,会话对象将变为null

C# 对于某些无效请求,会话对象将变为null,c#,url,session,request,C#,Url,Session,Request,我有一个混合(使用MVC和经典ASP页面)ASP(C#)Net应用程序 并且需要为MVC和遗留代码实现通用错误处理; 也就是说,我必须检测无效的URL并将无效的请求重新路由到 主页或登录页(取决于用户是否登录) 我在“应用程序错误”中添加了错误处理代码(请参阅下面的代码) 问题如下:loggedin用户id保存在会话对象中 对于某些无效URL,会话对象将变为null,并显示:“会话状态在此上下文中不可用” 例如: 对于以下URL,存在会话对象: 但对于以下URL,会话对象为空: 所以,问题是当我

我有一个混合(使用MVC和经典ASP页面)ASP(C#)Net应用程序 并且需要为MVC和遗留代码实现通用错误处理; 也就是说,我必须检测无效的URL并将无效的请求重新路由到 主页或登录页(取决于用户是否登录)

我在“应用程序错误”中添加了错误处理代码(请参阅下面的代码)

问题如下:loggedin用户id保存在会话对象中 对于某些无效URL,会话对象将变为null,并显示:“会话状态在此上下文中不可用”

例如:

对于以下URL,存在会话对象:

但对于以下URL,会话对象为空:

所以,问题是当我在请求中拼错文件夹名称时,会话对象为什么会变为null,以及如何解决这个问题

路由图如下所示:

context.MapRoute(
    "default",
    "test/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);


protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    Response.Clear();

    HttpException httpException = exception as HttpException;
    if (httpException != null) // Http Exception
    {
        switch (httpException.GetHttpCode())
        {
            case 400: // Bad Request
            case 404: // Page Not Found
            case 500: // Internal Server Error
                {
                    // Clear the error on server.
                    Server.ClearError();

                    ServerConfiguration scfg = ServerConfiguration.Instance;
                    if (ConnxtGen.App.AppUtility.GetCurrentUserID() != -1)
                    {
                        Response.RedirectToRoute("Unity_default", new { controller = "Home", action = "Index" });
                    }
                    else
                    {
                        Response.Redirect(scfg.PagePath + "/login/login.aspx", false);
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;
}

您应该了解的一点是,会话变量仅在HttpApplication.AcquisiteRequestState事件发生后可用

在您的问题中,在Asp.Net页面生命周期的过程中,您希望在会话中获取一些信息的时机还太早

我发现这篇文章肯定会更好地解释会话对象何时可用:

下面是一篇很棒的文章,它更详细地解释了Asp.Net页面生命周期的整个内部过程:


您应该了解的一点是,会话变量仅在HttpApplication.AcquisiteRequestState事件发生后可用

在您的问题中,在Asp.Net页面生命周期的过程中,您希望在会话中获取一些信息的时机还太早

我发现这篇文章肯定会更好地解释会话对象何时可用:

下面是一篇很棒的文章,它更详细地解释了Asp.Net页面生命周期的整个内部过程:

 4. http://myserver:49589/te
context.MapRoute(
    "default",
    "test/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);


protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    Response.Clear();

    HttpException httpException = exception as HttpException;
    if (httpException != null) // Http Exception
    {
        switch (httpException.GetHttpCode())
        {
            case 400: // Bad Request
            case 404: // Page Not Found
            case 500: // Internal Server Error
                {
                    // Clear the error on server.
                    Server.ClearError();

                    ServerConfiguration scfg = ServerConfiguration.Instance;
                    if (ConnxtGen.App.AppUtility.GetCurrentUserID() != -1)
                    {
                        Response.RedirectToRoute("Unity_default", new { controller = "Home", action = "Index" });
                    }
                    else
                    {
                        Response.Redirect(scfg.PagePath + "/login/login.aspx", false);
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;
}