开发者

Object reference null- URL rewriting asp.net

开发者 https://www.devze.com 2023-02-20 21:04 出处:网络
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:

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)
{


开发者_JAVA百科    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");
    }
 }


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.


I think you should just leave out your call to context.CompleteRequest();

This is normally meant to stop the execution of a request, but you're calling it when your application is initializing and no requests are being processed. My guess was that in .NET 2.0 it would tolerate this call and not do anything bad, but in a later version it blows up.

It doesn't look to me like you want to stop the request immediately after you've rewritten the URLs... otherwise, why even rewrite them? So just try getting rid of that method call.


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(); 
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号