2016-01-22 23 views
0

我在ASP WebForms中遇到問題,其中HttpContext.Current.Session爲空,如果我的請求經過自定義IRouteHandlerWeb窗體 - 通過自定義IRouteHandler時會話對象爲null

我見過很多答案,人們在如何訪問自定義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; 
    } 
} 

有沒有人有一個想法?

謝謝

回答

0

這是一個配置問題。 AppPool被設置爲v4.0集成,而它應該是v2.0 Classic。

相關問題