Web窗体 - 通过自定义IRouteHandler时,Session对象为null

时间:2016-01-22 10:48:38

标签: asp.net session webforms iroutehandler

我在ASP WebForms中遇到问题,如果我的请求通过自定义HttpContext.Current.Session,则IRouteHandler为空。

我看到很多答案,人们就如何在自定义IRouteHandler中访问Session对象提出建议,但这不是我的问题。我不需要访问处理程序中的会话对象,我只是不希望它在使用该处理程序时为null。

这是自定义处理程序的代码:

public class LanguageRouteHandler : IRouteHandler
{
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Logger.Debug("GetHttpHandler");

        //return new LanguageHttpHandler(this, requestContext);

        string rest = requestContext.RouteData.Values["rest"] as string;
        string destinationPath = "";

        string language = requestContext.RouteData.Values["language"] as string;

        string queryString = "?";
        NameValueCollection queries = HttpContext.Current.Request.QueryString;

        foreach (string myKey in queries.AllKeys)
        {
            queryString += (myKey + "=" + queries[myKey] + "&");
        }
        HttpContext.Current.Items["qs"] = queryString.Substring(0, queryString.Length - 1);
        if (rest == null || rest == "")
        {
            HttpContext.Current.Items["lng"] = language;

            destinationPath = "~/Content.aspx";
            EasyUrlHelper.Current.OriginalPath = destinationPath.Replace("~/", "");

            HttpContext.Current.RewritePath(string.Concat("~/Content.aspx", queryString == "?"));
            return BuildManager.CreateInstanceFromVirtualPath("~/Content.aspx", typeof(Page)) as Page;
        }
        else
        {
            if (rest.EndsWith("aspx"))
            {
                HttpContext.Current.Items["lng"] = language;

                if (!rest.StartsWith("~/"))
                    destinationPath = "~/" + rest;
                else
                    destinationPath = rest;

                EasyUrlHelper.Current.OriginalPath = destinationPath.Replace("~/", "");

                HttpContext.Current.RewritePath(string.Concat(destinationPath, queryString == "?" ? "" : queryString));
                return BuildManager.CreateInstanceFromVirtualPath(destinationPath, typeof(Page)) as Page;
            }

        }

        return BuildManager.CreateInstanceFromVirtualPath("~/404.aspx", typeof(Page)) as Page;
    }
}

有没有人有想法?

由于

1 个答案:

答案 0 :(得分:0)

这是一个配置问题。 AppPool设置为v4.0集成,而它应该是v2.0 Classic。