具有自定义 HTML 帮助程序的 Spark View 引擎

发布于 2024-09-11 13:01:16 字数 987 浏览 3 评论 0原文

我已将一些自己的帮助程序添加到项目中的 System.Web.Mvc 中,并使其与默认的 asp.net mvc 视图引擎一起使用。通过定义助手,就像

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);
        }
    }
}

我使用 <%= Html.h("

some string

") %>

现在我正在使用 Spark 视图引擎一样似乎无法让它发挥作用。我收到以下错误:

“System.Web.Mvc.HtmlHelper”不包含“h”的定义,并且找不到接受“System.Web.Mvc.HtmlHelper”类型的第一个参数的扩展方法“h”(您是否缺少using 指令还是程序集引用?)'

如何让 Spark 查看其他帮助程序?

编辑:我还添加了 _global.spark 与 无济于事

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第几種人 2024-09-18 13:01:16

当我的项目全面展开时,我的 _global.spark 通常最终看起来像这样。我建议在一开始就这样做以避免这些问题:

<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" />

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" />
む无字情书 2024-09-18 13:01:16

确保 System.Web.Mvc.HtmlHelper 已在 Spark 配置部分的 web.config 中注册。

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

A君 2024-09-18 13:01:16

在 _global.spark 文件中添加 似乎已经解决了这个问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文