0

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

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

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

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;
    }
}

有人有想法吗?

谢谢

4

1 回答 1

0

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

于 2016-01-22T13:10:25.653 回答