Spark View Engine with custom HTML Helpers
-
01-10-2020 - |
Pergunta
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
Solução
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" />
Outras dicas
Make sure that System.Web.Mvc.HtmlHelper
is registered in your web.config within the spark config section.
Added <using namespace="System.Web.Mvc" />
in the _global.spark file seems to have solved this problem.