title: StructureMap causes Stack Empty exception in Web API Help Pages ModelDescriptionLink.cshtml
body: <p>I have a Web API project which uses StructureMap for its DI. It's been working fine for awhile, but I'm having some issues with the Web API help pages (Microsoft.AspNet.WebApi.HelpPage) where InvalidOperationExceptions are being thrown as a result of an empty stack. </p>
  
  <p>I created a new Web API project with the help pages, and it works fine until I add the StructureMap.WebApi2 package, whereas the previously mentioned exception is being thrown here, inside ModelDescriptionLink.cshtml</p>
  
  <pre><code>else if (modelDescription is CollectionModelDescription)
  {
      var collectionDescription = modelDescription as CollectionModelDescription;
      var elementDescription = collectionDescription.ElementDescription;
      @:Collection of @Html.DisplayFor(m =&gt; elementDescription.ModelType, "ModelDescriptionLink", new { modelDescription = elementDescription })
  }
  else
  {
      @Html.DisplayFor(m =&gt; modelDescription)
  }
  </code></pre>
  
  <p>It's being thrown at <code>@:Collection of @Html.DisplayFor(m =&gt; elementDescription.ModelType, "ModelDescriptionLink", new { modelDescription = elementDescription })</code> when it tries to display the link to the resource description for his this model.</p>
  
  <p>This is a slimmed down route that still causes the exception:</p>
  
  <pre><code>[Route("Test")]
  public IHttpActionResult Post([FromBody] IEnumerable&lt;MySimpleModel&gt; models)
  {
      return null;
  }
  </code></pre>
  
  <p>Attempting to visit the documentation for this route at <code>http://localhost:21966/Help/Api/POST-Test</code> causes the exception:</p>
  
  <p><img src="https://i.stack.imgur.com/irFU3.png" alt="Exception image"></p>
  
  <p>I was only able to find one <a href="https://twitter.com/modul8com/status/492303789899407361" rel="noreferrer">example of someone having the same problem</a> and their solutions were to switch from StructureMap to Ninject or to avoid the exception with null checks. </p>
  
  <p>Here's the top of the stack trace:</p>
  
  <pre><code>[InvalidOperationException: Stack empty.]
  System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
  System.Collections.Generic.Stack`1.Peek() +6693321
  System.Web.WebPages.WebPageBase.get_Output() +51
  System.Web.WebPages.WebPageBase.GetOutputWriter() +35
  System.Web.WebPages.WebPageExecutingBase.BeginContext(String virtualPath, Int32 startPosition, Int32 length, Boolean isLiteral) +50
  ASP._Page_Areas_HelpPage_Views_Help_DisplayTemplates_ModelDescriptionLink_cshtml.Execute() in c:...ModelDescriptionLink.cshtml:28
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
  System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +145
  System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
  System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
  System.Web.Mvc.Html.ActionCacheViewItem.Execute(HtmlHelper html, ViewDataDictionary viewData) +278
  </code></pre>
  
  <p>By catching the exception in this place, it pops up later on in <code>HelpPageApiModel.cshtml</code> on a nearly identical line: <code>@Html.DisplayFor(m =&gt; m.ResourceDescription.ModelType, "ModelDescriptionLink", new { modelDescription = Model.ResourceDescription })</code>. This is the top of that stack trace:</p>
  
  <pre><code>[InvalidOperationException: Stack empty.]
  System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
  System.Collections.Generic.Stack`1.Pop() +6667365
  System.Web.WebPages.WebPageBase.PopContext() +66
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +154
  </code></pre>