Spark View Engine with custom HTML Helpers

匆匆过客 提交于 2019-12-24 11:27:26

问题


I've added some of my own helpers to the System.Web.Mvc within my project and got it working with the default asp.net mvc view engine. By defining the helper like

namespace System.Web.Mvc
{
    public static class XSSHelper
    {
        public static string h(this HtmlHelper helper, string input)
        {
            return AntiXss.HtmlEncode(input);
        }

        public static string Sanitize(this HtmlHelper helper, string input)
        {
            return AntiXss.GetSafeHtml(input);
        }

        public static string hscript(this HtmlHelper helper, string input)
        {
          return AntiXss.JavaScriptEncode(input);
        }
    }
}

I called it using <%= Html.h("<h1>some string</h1>") %>

Now that I am using the spark view engine I cannot seem to get this to work. I receive the following error:

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'h' and no extension method 'h' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)'

How can I get Spark to see the additional helpers?

EDIT: I've also added _global.spark with <using namespace="myApp" /> to no avail


回答1:


My _global.spark usually ends up looking like this by the time my project is in full swing. I recommend just doing this at the beginning to avoid these issues:

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

<use assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<use assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<use assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<use assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<use assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<use assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<use assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<use assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />



回答2:


Make sure that System.Web.Mvc.HtmlHelper is registered in your web.config within the spark config section.




回答3:


Added <using namespace="System.Web.Mvc" /> in the _global.spark file seems to have solved this problem.



来源:https://stackoverflow.com/questions/3350517/spark-view-engine-with-custom-html-helpers

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