我最近在與實際站點相同的服務器上設置了我的個人網站的新實例。新實例是「最新」配置,因此它具有我正在開發的最新版本的所有新功能。我已經將目標框架的最新版本從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,它仍然導致同樣的問題。此外,這似乎是發生有或沒有訪問者登錄,而不僅僅在成員頁面上。
好奇:在你提供的代碼中,哪一行是第39行? – 2010-01-10 14:57:49