我最近在与实际站点相同的服务器上设置了我的个人网站的一个新实例。新的实例是一个“最新的”配置,所以它包含了我正在开发的最新版本的所有新特性。在最新的版本中,我将Target Framework从2.0版本切换到了3.5版本,这是编译后的项目中唯一真正重要的变化。
在所有成员页面和其他一些页面以及一些用户控件上,我调用了这两个项目中的一个或两个,但每个项目都失败并导致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但请注意,这只发生在服务器2008上的IIS 7中,该站点在Visual Studio 2008中工作得很好。我甚至将IIS中的主网站附加到这个目录中,它也做同样的事情。这是怎么可能发生的,原因是什么?
为了方便起见,我将目标框架改回了2.0版本,但它仍然会导致同样的问题。而且,这似乎发生在访问者登录或不登录的情况下,而不仅仅是在成员页面上。
发布于 2010-01-10 23:08:02
HttpContext.Current.User或user.ProviderUserKey为null。
我猜是前者。哪一行是39行?
另外,是否有用户登录?
发布于 2010-01-10 23:19:30
您为IIS安装了哪些身份验证模型?默认情况下,只有匿名才可用,这可能会将HttpContext.Current.User设置为null。
https://stackoverflow.com/questions/2037347
复制相似问题