ASP MVC.NET3 Local IIS7 Object Reference Error

During the development of the mvc web application, I deal with problems starting a local site instance. When I try to reload the page, after a successful first load, I see an error below. If I started the site through the virtual server VS, there are no problems. My application pool works in integrated mode and works .net 4. Any idea why this is happening? Is there enough information?

[NullReferenceException: the reference to the object is not installed in the instance of the object.] System.Web.HttpServerVarsCollection.Get (string name) +109 System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewritten (HttpContextBase httpContext) +59 System.WebHMvc.Pvppers .GenerateClientUrlInternal (HttpContextBase httpContext, String contentPath) +213 System.Web.Mvc.PathHelpers .UI.Helpers.MenuHelper.GenerateUrl (string URL) in C: \ Development \ Hg \ LeadManager \ Web.UI \ Helpers \ MenuHelper.cs: 1132 LeadManager.Web.UI.Helpers.MenuHelper.BuildLeadManagementMenu (agent menu navMenu, 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 (WebPageContextContext page, writer WebWebRange.WebPage.WebPerBebReb. WebPages.WebPageBase.Write (HelperResult result) +88 System.Web.WebPages.WebPageBase.RenderSurrounding (String partialViewName, Action 1 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, Func <> c__DisplayClass1c. <InvokeActionResultWithFilters> b__19 () +32 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter (IResultFilter filter, ResultExecutingContext preContext, Func 1 rodolzhenie) +748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter (IResultFilter filter, ResultExecutingContext preContext, Func 1 continuation) +748196 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList IList 1 filter, 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 (step IExecutionStep, Boolean & completed4 synchronously)

MenuHelper is used to create navigation menus. Code failure is a return statement outside of 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)); } 
+4
source share
3 answers

I found the problem, it was an IIS7 URL rewriter module. I'm not sure why and how, but I solved the problem by deleting it.

+4
source

I was getting this problem too, and really removing the Rewrite module of the IIS7 URL will help fix this. The problem is that we always had the Rewrite module installed on all of our servers, which made me a little nervous. So I looked at it a little more to see what I changed to trigger this, and it turned out that it was me. In fact, I did this several times earlier and, it seems, did not study, because it is easy to do.

My class took in UrlHelper as a dependency through a constructor like

 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 he was supposed to look like he served the right UrlHelper for the request, and not already located, which was created as a singleton when the site was first hit.

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

It will take some time to notice these things, I need to come up with a test to ensure that UrlHelper is not served in singleton mode.

TL: dr

Binding my Ninject to the class that assigned UrlHelper as a dependency through its constructor was SingletonScope() instead of RequestScope() , which is very bad for obvious reasons ..

In any case, I hope this helps someone (or me in the future).

+1
source

So this is a line that does not work:

 return (new UrlHelper(instance.htmlHelper.ViewContext.RequestContext)).Content(url); 

We know that the instance is not null due to the line above, which already checks for null. Exceptional link exceptions are the most common unhandled exceptions and are also easiest to avoid. In general, you should never assume that properties or parameters that are null types are not null. I assume instance.htmlHelper is your problem. I assume htmlHelper is an instance of System.Web.Mvc.HtmlHelper. Therefore, ViewContext and RequestContext should not be null if it is a valid HtmlHelper created by MVC.

A simple solution is to check for zeros like:

 if (instance.htmlHelper == null) { throw new InvalidOperationException("htmlHelper has not been populated."); } return (new UrlHelper(instance.htmlHelper.ViewContext.RequestContext)).Content(url); 

Obviously, you could repeat this for ViewContext and RequestContext using MVC to populate them.

Now, assuming htmlHelper is null (the most likely situation), here you have a slight design problem. What you really have to do is extend the HtmlHelper class, not use a static method. Also, it looks like you are using some kind of static instance, which is probably also invalid in MVC. MVC typically creates controller instances and views for each request. You can still use the static method to implement web forms, but for MVC you have to extend the HtmlHelper like this:

 public static MvcHtmlString GenerateUrl(this htmlHelper, string url) { return (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Content(url); } 

Make sure you always use the active HtmlHelper from MVC, and not some static instance. I'm not sure if ViewContext or RequestContext can be empty after the request is completed. I have never tried to save an HtmlHelper after a single request. Even if they are not null, otherwise they may be invalid and will cause problems below in the MVC pipeline.

Hope this helps.

-one
source

Source: https://habr.com/ru/post/1392814/


All Articles