【问题标题】:encode angular template in ASP.NET mvc在 ASP.NET mvc 中编码角度模板
【发布时间】:2014-08-25 06:11:25
【问题描述】:

我正在尝试编码这个简单的属性...

var url = Url.Action("Method", new { code = "{{item.code}}" });
<a ng-href="@url">link</a>

我需要完整的 MVC 框架输出的角度模板分隔符。
唯一似乎有效的是:

@Html.Raw("/a/b/c/d/{{item.code}}")

这种方法的问题是我失去了从Url.Action 获得的所有实用程序。我想尽可能利用路由。

应该有一种方法可以对{{item.code}} 进行编码,这样它就不会被转换。

【问题讨论】:

    标签: c# asp.net-mvc angularjs templates


    【解决方案1】:

    我有类似的问题 w jsView。这就是它对我有用的方式。扩展方法:

    public static IHtmlString RawLink(this HtmlHelper helper, string link)
    {
        return helper.Raw(HttpUtility.UrlDecode(link));
    }
    
    public static IHtmlString RawActionLink(
        this HtmlHelper helper,
        string actionName,
        string controllerName,
        object routeValues)
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        return RawLink(helper, urlHelper.Action(actionName, controllerName, routeValues));
    }
    

    用法:

    <a href="@Html.RawLink(Url.Action("Action", "Controller", new { id = "{{:Id}}" }))">Some link</a>
    

    或:

    <a href="@Html.RawActionLink("Action", "Controller", new { id = "{{:Id}}" })">Some link</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多