Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

when i use HTTP module for URL rewriting , i get this error:

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpmodules> section in the application configuration.

My URL rewriting code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for URLRewriter
/// </summary>
public class URLRewriter : IHttpModule

    #region IHttpModule Members

    /// <summary>
    /// Dispose method for the class
    /// If you have any unmanaged resources to be disposed
    /// free them or release them in this method
    /// </summary>
    public void Dispose()
        //not implementing this method
        //for this example

    /// <summary>
    /// Initialization of the http application instance
    /// </summary>
    /// <param name="context"></param>
    public void Init(HttpApplication context)
        context.BeginRequest += new EventHandler(context_BeginRequest);
    /// <summary>
    /// Event handler of instance begin request
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void context_BeginRequest(object sender, EventArgs e)
        //Create an instance of the application that has raised the event
        HttpApplication httpApplication = sender as HttpApplication;

        //Safety check for the variable httpApplication if it is not null
        if (httpApplication != null)
            //get the request path - request path is    something you get in
            //the url
            string requestPath = httpApplication.Context.Request.Path;

            //variable for translation path
            string translationPath = "";

            //if the request path is /urlrewritetestingapp/laptops/dell/
            //it means the site is for DLL
            //else if "/urlrewritetestingapp/laptops/hp/"
            //it means the site is for HP
            //else it is the default path
            switch (requestPath.ToLower())
                case "/laptops/dell/":
                    translationPath = "/showitem.aspx?itemid=7";
                case "/laptops/hp/":
                    translationPath = "/showitem.aspx?itemid=8";
                    translationPath = requestPath.ToLower();

            //use server transfer to transfer the request to the actual translated path


My Config Code :
<?xml version="1.0"?>
  For more information on how to configure your ASP.NET application, please visit
      <add name="URLRewriter" type="URLRewriter"/>
    <validation validateIntegratedModeConfiguration="false" />
    <add name="conn" connectionString="Data Source=.;Initial Catalog=CMS;User ID=sa;Password=123456" />

      <compilation debug="true" targetFramework="4.0">

          <add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add assembly="System.Design, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

      <httpRuntime targetFramework="4.0"/>
    <pages enableSessionState="true" />
    <sessionState mode="StateServer"></sessionState>
      <httpModules >
        <add name="URLRewriter" type="URLRewriter"/>
        <add type="System.Web.SessionState.SessionStateModule" name="Session" />

    <add key="webpages:Enabled" value="true"/>


Please Help Me . Thank You.
Dholakiya Ankit 7-Aug-13 1:19am    
which line you are getting error?

1 solution

<pages enableSessionState="true" />

in your web.config

Share this answer
adriancs 6-Aug-13 13:03pm    
He had already added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900