I am trying to enable Session in the GettHttpHandler method for my IRouteHandler classes, but the session is always zero. Can someone tell me what I am doing wrong?
In global.asax, I have
RouteTable.Routes.Add("All", new Route("{*page}", new MyRouteHandler()));
The MyRouteHandler class, where Session is null, looks like this:
public class MyRouteHandler : IRouteHandler, IRequiresSessionState { public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) { string test = HttpContext.Current.Session["test"].ToString(); return BuildManager.CreateInstanceFromVirtualPath("~/Page.aspx", typeof(Page)) as Page; } }
I made a small test application that shows the problem.
Can someone tell me what I am doing wrong?
Edited to add:
Yes, I really need session data in the route handler. There are many reasons, but it is easy to explain when the user can switch to viewing the site in preview mode.
The site consists of a hierarchy of dynamic pages (/ page1 / page2 ...) in the database, which can be published normally or for preview. The creator of the content viewing the site can choose to view only regular pages, as well as published for preview. The view mode is saved in the user session, so in order to process the requested page, the route handler must know the view mode.
Therefore, I really need a session already at this stage.
source share