Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC.NET3 Local IIS7 object reference error

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 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, Func1 continuation) +748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +265 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));
}
like image 875
Drew Avatar asked Oct 08 '22 22:10

Drew


2 Answers

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.

like image 149
Drew Avatar answered Oct 12 '22 12:10

Drew


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

public class MyClass : IMyInterface
{
   private readonly UrlHelper _url;

   public MyClass(UrlHelper url)
   {
      _url = url;
   }

   public RedirectResult RedirectMeSomewhere()
   {
      return new RedirectResult(_url.Action("MyAction", "MyController"));
   }
}

and my Ninject binding looked like this

Bind<IMyInterface>()
   .To<MyClass>()
   .InSingletonScope();

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.

Bind<IMyInterface>()
   .To<MyClass>()
   .InRepositoryScope();

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 of RequestScope(), which is very bad for obvious reasons..

Anyways, hope that helps someone (or me in the future).

like image 30
Siy Williams Avatar answered Oct 12 '22 10:10

Siy Williams