2010-01-10 50 views
3

我最近在與實際站點相同的服務器上設置了我的個人網站的新實例。新實例是「最新」配置,因此它具有我正在開發的最新版本的所有新功能。我已經將目標框架的最新版本從2.0改爲3.5,這是唯一真正的主要變化,就像編譯過的項目一樣。HttpContext僅在IIS 7中導致對象引用錯誤

在所有成員頁面和其他一些頁面以及一些用戶控件上,我調用了這兩個項目中的一個或兩個,並且每個都失敗並導致錯誤Object reference not set to an instance of an object。但不是在每一頁上!

public static string CurrentUserName 
    { 
     get 
     { 
      string userName = ""; 
      if (HttpContext.Current.User.Identity.IsAuthenticated) 
       userName = HttpContext.Current.User.Identity.Name; 
      return userName; 
     } 
    } 

    public static string CurrentUserID 
    { 
     get 
     { 
      string userID = ""; 
      if (HttpContext.Current.User.Identity.IsAuthenticated) //Line 39 
      { 
       MembershipUser user = Membership.GetUser(); 
       userID = user.ProviderUserKey.ToString(); 
      } 
      return userID; 
     } 
    } 

隨着堆棧跟蹤之中:

[NullReferenceException: Object reference not set to an instance of an object.] 
    isClasses.BizObjects.get_CurrentUserID() in C:\My Dropbox\Trunk\Classes\BizObjects.cs:39 
    TheImageStore_Profile..ctor() in w:\dev\Member\Profile.aspx.cs:9 
    ASP.member_profile_aspx..ctor() in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\faba6118\7249af47\App_Web_profile.aspx.48943a5b.w8t4pvz5.0.cs:0 
    __ASP.FastObjectFactory_app_web_profile_aspx_48943a5b_w8t4pvz5.Create_ASP_member_profile_aspx() in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\faba6118\7249af47\App_Web_profile.aspx.48943a5b.w8t4pvz5.2.cs:0 
    System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32 
    System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119 
    System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33 
    System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path) +37 
    System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +307 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

但是你要知道,這僅僅是在IIS 7 Server 2008上發生的事情,是從Visual Studio 2008可以正常使用該網站我甚至附在IIS中的主要網站到這個目錄,它做同樣的事情。怎麼可能發生這種情況,原因是什麼?

只是爲了踢我改變了目標框架回到2.0,它仍然導致同樣的問題。此外,這似乎是發生有或沒有訪問者登錄,而不僅僅在成員頁面上。

+1

好奇:在你提供的代碼中,哪一行是第39行? – 2010-01-10 14:57:49

回答

1

HttpContext.Current.Useruser.ProviderUserKeynull

我會猜測前者。哪一行是第39行?

此外,有沒有用戶登錄?

+0

是的,有一個登錄的用戶。這似乎來自profile.aspx上的用戶控件。但是同一個用戶控件在主頁上加載得很好(default.aspx),但是它又無法加載到其他一些常用頁面上。第39行是'if(HttpContext.Current.User.Identity.IsAuthenticated)' – 2010-01-10 15:16:38

+0

取一個失敗的頁面,刪除控件,添加'<%= HttpContext.Current.User%>'並查看它說了什麼。 – SLaks 2010-01-10 15:23:15

+0

我將它添加到失敗的頁面,我什麼都沒有。但是,當我將它添加到正常工作的頁面時,它會在頁面的頂部給我「System.Web.Security.RolePrincipal」。失敗的頁面加載正確,使用此代碼的用戶控件將被刪除。 – 2010-01-10 15:40:20

0

您爲IIS安裝了哪些身份驗證模型?默認情況下,它只是匿名的,可以將HttpContext.Current.User設置爲null。

+0

我的一部分想排除IIS作爲問題,因爲我將主網站(在現有代碼中正確使用這些相同的項目)連接到此目錄,並且仍然失敗。這不是新的代碼,它已經在網站上一段時間了,因此它已經被使用了。 – 2010-01-10 15:24:32

+0

哦,順便說一句,表單和匿名啓用。表單具有使用設備配置文件的模式 – 2010-01-10 15:44:00

相關問題