HttpContext 仅在 IIS 7 中导致对象引用错误
我最近在与实际网站相同的服务器上设置了个人网站的新实例。新实例是“最新”配置,因此它具有我正在开发的最新版本的所有新功能。在最新的版本中,我将目标框架从 2.0 切换到 3.5,这是与编译项目一样唯一真正重大的变化。
在所有成员页面和其他一些页面以及一些用户控件上,我调用这两项中的一项或两项,但每一项都失败并导致对象引用未设置为对象的实例
错误。但不是每页都有!
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
但请注意,这仅发生在 Server 2008 上的 IIS 7 中,该站点在 Visual Studio 2008 中完美运行。我什至将 IIS 中的主网站附加到此目录,它执行相同的操作。怎么可能发生这种情况,原因可能是什么?
只是为了好玩,我将目标框架更改回 2.0,但它仍然导致相同的问题。此外,无论访客是否登录,这种情况似乎都会发生,而不仅仅是在会员页面上。
I recently set up a new instance of my personal website on the same server as the actual site. The new instance is a "latest" configuration so it's got all the new features of the latest build I'm working on. The latest build I've switched the Target Framework to 3.5 from 2.0 as the only really major change as fas as the compiled items go.
On all member pages and some other pages and some user controls I call one or both of these two items, and each are failing and causing an Object reference not set to an instance of an object
, error. But not on every page!
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;
}
}
With the stack trace being:
[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
But mind you, this is only happening in IIS 7 on Server 2008, the site is working perfectly from Visual Studio 2008. I've even attached the main website in IIS to this directory and it does the same thing. How is it possible to have this happen, and what could be the cause?
Just for kicks I changed the Target Framework back to 2.0 and it's still causing the same issue. Also this seems to be happening with or without the visitor being logged in, and not just on member pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HttpContext.Current.User
或user.ProviderUserKey
为null
。我猜是前者。 39号线是哪条线?
另外,是否有用户登录?
Either
HttpContext.Current.User
oruser.ProviderUserKey
isnull
.I would guess the former. Which line is line 39?
Also, is there a user logged in?
您为 IIS 安装了哪些身份验证模型?默认情况下,只有匿名可用,并且可以将 HttpContext.Current.User 设置为 null。
What authentication models have you installed for IIS? By default it is only anonymous that is available and that may set HttpContext.Current.User to null.