Search code examples
asp.neturl-rewritingobject-reference

Object reference null- URL rewriting asp.net


I have developed my asp.net website in .NET 2.0 in other system where it is working fine. Now when I copied the asp.net website in my system and run it than I am getting the run time error:

Object reference not set to an instance of an object.

 public class FixURLs : IHttpModule 
{
    public FixURLs()
    {

    }

    #region IHttpModule Members

    public void Dispose()
    {
        // do nothing
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.CompleteRequest(); 

    }

 ..... some other logic

I am getting object reference error at the line:

context.CompleteRequest();

My web.Config file has

<compilation debug="true">
  <assemblies>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>

How can I fix this issue?

EDIT Edit Note New code added

 void context_BeginRequest(object sender, EventArgs e)
{


    HttpApplication app = (HttpApplication)sender;

    if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
    {
        app.Context.RewritePath("BikeInfo.aspx", "", "");
    }
    else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
    {
        app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
    }
 }

Solution

  • I strongly suspect that you would want to put completerequest at the end of the context_beginrequest method because right now this doesn't really make sense. If that isn't the case please post that method as well so it's clear what you are trying to do.

    EDIT: It looks like your intention is to do this:

     void context_BeginRequest(object sender, EventArgs e)
    {
    
        HttpApplication app = (HttpApplication)sender;
    
        if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
        {
            app.Context.RewritePath("BikeInfo.aspx", "", "");
            app.CompleteRequest(); 
        }
        else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
        {
            app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
            app.CompleteRequest(); 
        }
     }
    

    It doesn't look like you'd want to call CompleteRequest unless you are actually doing something in BeginRequest. And to be clear, in your original code, you are calling CompleteRequest before the BeginRequest event even fires.