0
我在ASP WebForms中遇到問題,其中HttpContext.Current.Session
爲空,如果我的請求經過自定義IRouteHandler
。Web窗體 - 通過自定義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;
}
}
有沒有人有一個想法?
謝謝