1

Currently my product page URL is like

http://www.localhost:80/products/default.aspx?code=productCode

I want to access product page with

http://www.localhost:80/productCode

I have used HTTP module for this.

public class UrlRewritingModule : IHttpModule

{

        public void Dispose()
        {

        }
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

           context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);


        }  
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
             HttpContext context = ((HttpApplication)sender).Context;
            if (some condition)
            {
               context.RewritePath(url);
             }
        }
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //We set back the original url on browser
            HttpContext context = ((HttpApplication)sender).Context;

            if (context.Items["originalUrl"] != null)
            {
                context.RewritePath((string)context.Items["originalUrl"]);
            }
        }
}

I have register it in web.config and it is working fine. But when I deploy it in IIS that session and application variables are not throwing null referent Exceptions.

Can anyone help me?

Edit: Do it require extra code to access session/ Application variable for rewritten URLs ?

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Pradip Prajapati
  • 152
  • 1
  • 4
  • 18

2 Answers2

0

Have you tried using HTTPContext.Current?

maralfol
  • 178
  • 6
  • Maybe this will help you http://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule – maralfol Oct 15 '12 at 13:54
  • Thanks for the info.i have tried it but some how my session variable is still throwing null reference exception. If I try it to access product page directly then all work fine. If we use custom handler then do we need to write extra code to use session/application variables? – Pradip Prajapati Oct 16 '12 at 09:57
0

I was able to solve issue (accessing session and application variables in subsequent pages rewritten by custom handler) by adding runAllManagedModulesForAllRequests="true" attribute in modules in web.config.

Pradip Prajapati
  • 152
  • 1
  • 4
  • 18
  • This is a very bad solution, basically you are bypassing static file handling including cache to run through ASP.Net and all other modules and handlers. Why aren't you using routes? – user3285954 Dec 08 '14 at 20:35