I'm having an issue in ASP WebForms where HttpContext.Current.Session
is null if my request goes through a custom IRouteHandler
.
I have seen many answers where people are giving advices on how to access the Session object inside a custom IRouteHandler
but this is not my issue. I don't need to access the session object in the handler, I just don't want it to be null when using that handler.
This is the code for the custom handler:
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;
}
}
Does anyone have an idea ?
Thanks
It was a configuration issue. The AppPool was set as v4.0 Integrated while it should have been v2.0 Classic.