“The name 'Html' does not exist in the current context” exception

放肆的年华 提交于 2019-12-13 05:19:43

问题


I'm rendering Spark templates in a console application and I'm trying to use RenderPartial like so:

${Html.RenderPartial("_mypartial.spark", myData)}

but I'm getting this error when compiling the view:

The name 'Html' does not exist in the current context

In the Shared\_global.spark I have added this imports:

<use namespace="System" />
<use namespace="System.Web.Mvc" />
<use namespace="System.Web.Mvc.Html" />
<use namespace="System.Collections.Generic" />
<use namespace="System.Linq" />

Everything else renders fine otherwise, it's just that Html is not defined.

It it makes any difference, I'm rendering this with an embedded NancyFX engine.


回答1:


I believe the problem isn't within the view itself. I'm not familiar NancyFX engine, but all engines views should be using a base view. The default is in the web.config in the views folder:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">

Of course you're not using razor, this is simply a comparison where Html exists on System.Web.Mvc.WebViewPage. How you configure NancyFX, the configuration needs to define the one that supports the HtmlHelpers (that either come with MVC or NancyFX, I'm not sure).

After taking a look at Gibhub It looks very similar:

<system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase">

And the NancyRazorViewBase has HtmlHelpers on it so it sounds very much like a configuration issue.



来源:https://stackoverflow.com/questions/23354588/the-name-html-does-not-exist-in-the-current-context-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!