2011-03-31 53 views
4

我在.NET 2.0中開發了我的asp.net網站,在其他系統中工作正常。現在,當我抄在我的系統的asp.net網站並運行它比我收到運行時錯誤:對象引用null- URL重寫asp.net

context.CompleteRequest(); 

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 

我在該行獲得對象引用錯誤

我的web.config文件有

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

我該如何解決這個問題?

編輯 編輯注新的代碼添加

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

任何幫助,將不勝感激 – Chris 2011-04-02 07:20:46

+0

你在本地IIS或卡西尼開發Web服務器上運行呢? – 2011-04-06 05:54:19

+0

我需要在context_BeginRequest方法中看到代碼。你能發佈代碼嗎? – 2011-04-06 06:00:27

回答

4

我強烈懷疑,你會想要把completerequest在context_beginrequest方法結束,因爲現在這並沒有真正意義。如果情況並非如此,請發佈該方法,這很清楚你正在嘗試做什麼。

編輯:它看起來像你的目的是要做到這一點:

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

它看起來並不像你想打電話給CompleteRequest除非你是真的在做事情中的BeginRequest。並且要清楚的是,在您的原始代碼中,您在BeginRequest事件甚至觸發之前調用CompleteRequest。

+0

這就是我要發表的意見:) – 2011-04-06 06:05:20

+0

我已經更新了代碼,請檢查 – Chris 2011-04-06 06:40:10

0

我想你應該剛剛離開了您的來電context.CompleteRequest();

這通常意味着停止執行請求的,但你要調用它,當您的應用程序初始化並沒有請求正在處理中。我的猜測是,在.NET 2.0中,它可以容忍這個呼叫,並且不會做任何壞事,但在後來的版本中它會爆炸。

它沒有在我看來像你想要在重寫URL之後立即停止請求......否則,爲什麼要重寫呢?所以試着擺脫那個方法調用。

0

無效context_BeginRequest(對象發件人,EventArgs的){

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