3

Right now, I'm trying to work around an IE6/7 bug which requires the wrapping of the </a> closing tag with this IE specific comment to make some drop-down menu work:

<!--[if IE 7]><!--></a><!--<![endif]-->

Unfortunately, I cannot inject this directly into my View page code like this:

<%= Html.ActionLink("LinkName<!--[if IE 7]><!--></a><!--<![endif]-->","Action","Controller") %>

As Html.ActionLink will do the safe thing and filter out the comment to prevent a Javascript injection attack. Ok, cool. I'm fine with that. Good design decision.

What I'd like to do is write an Extension Method to this, but the process is eluding me as I haven't done this before.

I thought this would work, but Intellisense doesn't seem to be picking up this Extension method that I've written.

public static class MyLinkExtensions
{
    public static string ActionLinkIE(this HtmlHelper htmlHelper,  
        string linkText, string actionName, string controllerName)
    {
        return LinkExtensions.ActionLink(htmlHelper, linkText, actionName, controllerName).
            Replace(@"</a>", @"<!--[if IE 7]><!--></a><!--<![endif]-->");
    }
}

Any suggestions?

EDIT: Does the class name matter? (in my case, I've called it MyLinkExtensions)

Also, when mousing over <%= Html.ActionLink() %> that is appears to be an Extension Method already. Can I extend an Extension Method?

4

2 回答 2

6

您可能需要将 Extensions 命名空间放在 web.config 中:

<pages>      
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Linq" />
        <add namespace="Pretzel.Extensions.Html" />
      </namespaces>
</pages>

这应该有助于智能感知。

于 2010-06-16T18:34:07.127 回答
0

您可以根据来自 phil haack http://haacked.com/archive/2010/04/06/using-antixss-as-the-default-encoder-for-asp-net.aspx的这篇文章更改用于编码输出的库

这使您有机会使用 ANTiXss http://wpl.codeplex.com/。它是微软的一个库,具有:

  • Javascript编码
  • 白名单编码

几乎所有使用 ajax 和 javascript 的应用程序都需要 JavascriptEncoding,所以我认为没有这个库就没有机会。

如果助手使用此库,您只需将您的标签添加到白名单即可。

编辑

我刚刚在页面上看到这对现有的 Html Helper 不起作用:-(。好吧,我将这篇文章保留到 MVC3。希望 AnTiXss 包含在内,然后开箱即用。

于 2010-06-16T19:02:26.717 回答