ASP MVC.NET3 本地 IIS7 对象引用错误
在开发 mvc Web 应用程序期间,我遇到了运行站点本地实例的问题。当我尝试重新加载页面时,在首次加载成功后,我看到以下错误。如果我通过 VS 虚拟服务器运行该站点,则不会出现任何问题。我的应用程序池正在集成模式下运行,并且正在运行 .net 4。知道为什么会发生这种情况吗?这些信息足够吗?
[NullReferenceException:未将对象引用设置为对象的实例。] System.Web.HttpServerVarsCollection.Get(字符串名称)+109 System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewriting(HttpContextBase httpContext)+59 System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext,字符串contentPath)+213 System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext,字符串contentPath)+168 System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext,字符串 contentPath)+148 C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs:1132 中的 LeadManager.Web.UI.Helpers.MenuHelper.GenerateUrl(String url) C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs 中的 LeadManager.Web.UI.Helpers.MenuHelper.BuildLeadManagementMenu(菜单 navMenu,代理代理):554 C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs:530 中的 LeadManager.Web.UI.Helpers.MenuHelper.AddNavMenu(代理代理) c:\Development\Hg\LeadManager\Web\Views\Shared_Layout.cshtml 中的 ASP._Page_Views_Shared__Layout_cshtml.Execute():115 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +103 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)+172 System.Web.WebPages.WebPageBase.Write(HelperResult结果)+88 System.Web.WebPages.WebPageBase.RenderSurrounding(字符串partialViewName,操作
1主体)+233 System.Web.WebPages.WebPageBase.PopContext() +233 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+377 System.Web.Mvc.<>c__DisplayClass1c.
1延续)+748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter过滤器,ResultExecutingContext preContext,Funcb__19() +32 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter过滤器,ResultExecutingContext preContext,Func 1延续)+748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContextcontrollerContext,IList
1过滤器,ActionResultactionResult)+265 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContextcontrollerContext,字符串actionName)+748160 System.Web.Mvc.Controller.ExecuteCore() +159 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334 System.Web.Mvc.<>c_DisplayClassb.b_5() +62 System.Web.Mvc.Async。<>c_DisplayClass1.b_0() +15 System.Web.Mvc.<>c_DisplayClasse.b_d() +52 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +437 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean&completedSynchronously) +354
MenuHelper 用于构建导航菜单。失败的代码是 if 之外的 return 语句:
private static string GenerateUrl(string url)
{
if (instance == null)
{
// hack to enable using this on old web forms pages
return UrlHelper.GenerateContentUrl(url, new HttpContextWrapper(HttpContext.Current));
}
return (new UrlHelper(instance.htmlHelper.ViewContext.RequestContext)).Content(url);
}
private static string GenerateUrl(string actionName, string controllerName)
{
if (instance == null)
{
// hack to enable using this on old web forms pages
return GenerateUrl(String.Format("{0}/{1}", controllerName, actionName));
}
if (instance.htmlHelper == null)
throw new InvalidOperationException("htmlHelper has not been populated.");
if (instance.htmlHelper.ViewContext == null)
throw new InvalidOperationException("ViewContext has not been populated.");
if (instance.htmlHelper.ViewContext.RequestContext == null)
throw new InvalidOperationException("RequestContext has not been populated.");
UrlHelper urlHelper = new UrlHelper(instance.htmlHelper.ViewContext.RequestContext);
if (urlHelper == null)
throw new InvalidOperationException("UrlHelper has not been populated.");
if (String.IsNullOrEmpty(actionName))
throw new InvalidOperationException("actionName has not been populated.");
if (String.IsNullOrEmpty(controllerName))
throw new InvalidOperationException("controllerName has not been populated.");
return (urlHelper.Action(actionName, controllerName));
}
During development of an mvc web application, I'm encourtering issues running the local instance of the site. When I try to reload a page, after a successful first load, I'm seeing the below error. If I run the site through the VS virtual server, there are no issues. My app pool is running in integrated mode and it is running .net 4. Any idea why this is occurring? Is this enough information?
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.HttpServerVarsCollection.Get(String name) +109
System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext) +59
System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) +213
System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) +168
System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath) +148
LeadManager.Web.UI.Helpers.MenuHelper.GenerateUrl(String url) in C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs:1132
LeadManager.Web.UI.Helpers.MenuHelper.BuildLeadManagementMenu(Menu navMenu, Agent agent) in C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs:554
LeadManager.Web.UI.Helpers.MenuHelper.AddNavMenu(Agent agent) in C:\Development\Hg\LeadManager\Web.UI\Helpers\MenuHelper.cs:530
ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\Development\Hg\LeadManager\Web\Views\Shared_Layout.cshtml:115
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +103
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +172
System.Web.WebPages.WebPageBase.Write(HelperResult result) +88
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action1 body) +233
1 continuation) +748196
System.Web.WebPages.WebPageBase.PopContext() +233
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +377
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +32
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +748196
1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +748160
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334
System.Web.Mvc.<>c_DisplayClassb.b_5() +62
System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +15
System.Web.Mvc.<>c_DisplayClasse.b_d() +52
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +437
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +354
MenuHelper is used to build navigation menus. The code that fails is the return statement outside of the if:
private static string GenerateUrl(string url)
{
if (instance == null)
{
// hack to enable using this on old web forms pages
return UrlHelper.GenerateContentUrl(url, new HttpContextWrapper(HttpContext.Current));
}
return (new UrlHelper(instance.htmlHelper.ViewContext.RequestContext)).Content(url);
}
private static string GenerateUrl(string actionName, string controllerName)
{
if (instance == null)
{
// hack to enable using this on old web forms pages
return GenerateUrl(String.Format("{0}/{1}", controllerName, actionName));
}
if (instance.htmlHelper == null)
throw new InvalidOperationException("htmlHelper has not been populated.");
if (instance.htmlHelper.ViewContext == null)
throw new InvalidOperationException("ViewContext has not been populated.");
if (instance.htmlHelper.ViewContext.RequestContext == null)
throw new InvalidOperationException("RequestContext has not been populated.");
UrlHelper urlHelper = new UrlHelper(instance.htmlHelper.ViewContext.RequestContext);
if (urlHelper == null)
throw new InvalidOperationException("UrlHelper has not been populated.");
if (String.IsNullOrEmpty(actionName))
throw new InvalidOperationException("actionName has not been populated.");
if (String.IsNullOrEmpty(controllerName))
throw new InvalidOperationException("controllerName has not been populated.");
return (urlHelper.Action(actionName, controllerName));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以这是失败的行:
我们知道实例不为空,因为上面的行已经检查了空值。空引用异常是最常见的未处理异常,也是最容易避免的。一般来说,您永远不应该假定可空类型的属性或参数不为空。我的猜测是,instance.htmlHelper 是你的问题。我假设 htmlHelper 是 System.Web.Mvc.HtmlHelper 的实例。因此,如果 ViewContext 和 RequestContext 是由 MVC 创建的有效 HtmlHelper,则不应该为 null。
简单的解决方案是像这样检查 null:
显然,您可以对 ViewContext 重复此操作,并且 MVC 的 RequestContext 应该填充这些值。
现在假设 htmlHelper 为 null(最有可能的情况),这里有一个轻微的设计问题。您真正应该做的是扩展 HtmlHelper 类而不是使用静态方法。另外,您似乎正在使用某种静态实例,这在 MVC 中也可能无效。 MVC 通常为每个请求实例化控制器和视图。您仍然可以使用静态方法来实现 Web 表单,但对于 MVC,您应该像这样扩展 HtmlHelper:
确保您始终使用 MVC 中当前活动的 HtmlHelper,而不是某些静态实例。我不确定在请求完成后 ViewContext 或 RequestContext 是否可以为空。我从未尝试过在一个请求后保留 HtmlHelper。即使它们不为 null,它们也可能无效,并在 MVC 管道中进一步导致问题。
希望有帮助。
So this is the line that is failing:
We know that instance isn't null due to the line above that already checks for null. Null reference exceptions are the most common unhandled exceptions and are also the easiest to avoid. In general, you should never assume properties or parameters that are nullable types are not null. My guess is that instance.htmlHelper is your problem. I am assuming that htmlHelper is an instance of System.Web.Mvc.HtmlHelper. Therefore, ViewContext and RequestContext should not be null if that is a valid HtmlHelper created by MVC.
The simple solution is to check for nulls like this:
Obviously you could repeat this for ViewContext and RequestContext by MVC should populate those.
Now assuming htmlHelper is null (the most likely situation), you have a slight design problem here. What you should really be doing is extending the HtmlHelper class and not using a static method. Also, you seem to be using some kind of static instance which is probably also not valid in MVC. MVC typically instantiates controllers and views for each request. You can still use the static method for a web forms implementation, but for MVC you should extend HtmlHelper like this:
Make sure you are always using the currently active HtmlHelper from MVC and not some static instance. I'm not sure off the top of my head if ViewContext or RequestContext could be null after a request has been completed. I've never tried to persist an HtmlHelper after one request. Even if they aren't null, they could otherwise be invalid and be causing your problem further down in the MVC pipeline.
Hope that helps.
我找到了问题所在,是 IIS7 URL 重写模块。我不知道为什么或如何,但我通过卸载它解决了这个问题。
I found the issue, it was the IIS7 URL Rewrite Module. I'm not sure why or how but I solved the problem by uninstalling it.
我也遇到了这个问题,实际上卸载 IIS7 URL 重写模块将有助于解决它。问题是我们总是在所有服务器上安装重写模块,这让我有点紧张。于是我仔细研究了一下,看看我做了什么改变才导致了这个问题,结果发现是我造成的。事实上,我以前已经这样做过几次了,但似乎没有学会,因为这是一件很容易做的事情。
我的类通过构造函数将 UrlHelper 作为依赖项,就像这样
,我的 Ninject 绑定看起来像这样,
因为它应该看起来像这样,所以它为请求提供正确的 UrlHelper,而不是作为创建的已处置的 UrlHelper当网站第一次被访问时单身。
只是需要一段时间才能注意到这些事情,我需要进行一个测试以确保 UrlHelper 不会以单例形式提供。
tl:dr
我的 Ninject 类的绑定(通过其构造函数将 UrlHelper 作为依赖项)设置为
SingletonScope()
而不是RequestScope()
,由于显而易见的原因,这非常糟糕。无论如何,希望能帮助某人(或将来的我)。
I was getting this issue too, and indeed uninstalling the IIS7 URL Rewrite Module would help fix it. The problem is we've always had the Rewrite module installed on all our servers which made me a bit nervous. So I looked into it a bit more to see what I had changed to cause this, and it turned out it was me. In fact I've done this a few times before and don't seem to learn because it's an easy thing to do.
My class took in the UrlHelper as a dependency through the constructor like such
and my Ninject binding looked like this
where as it should have looked like this so it serves the correct UrlHelper for the request, instead of an already disposed one that was created as a singleton when the site was first hit.
It just takes a while to notice these things, I need to come up with a test to ensure that the UrlHelper isn't served up in a singleton.
tl:dr
My Ninject binding for a class that took the UrlHelper as a dependency through its constructor was set to
SingletonScope()
instead ofRequestScope()
, which is very bad for obvious reasons..Anyways, hope that helps someone (or me in the future).