【问题标题】:UrlHelper.Action: would like to generate a link that ends with '#something'UrlHelper.Action:想生成一个以“#something”结尾的链接
【发布时间】:2010-09-13 08:41:34
【问题描述】:

我正在尝试使用 Url.Action 创建一个链接,该链接以 #something 结尾;我认为路由值中有一些东西可以正确执行此操作,但我无法通过 Google 找到它。

到目前为止,我尝试了Url.Action("action", "controller", new {id="something", Area="area"})。生成的链接是预期的/action/controller/area,但最后我无法处理#something。

在网址方面,我可能会说<a href="<%= Url.Action(..) %>#something"> 就可以了,但这并没有让我觉得特别好;我正在寻找更好的解决方案。

【问题讨论】:

    标签: c# asp.net-mvc-2 urlhelper


    【解决方案1】:

    Url.Action() 方法没有为您执行此操作的重载。以太币你必须按照你建议的方式来做(只需在调用Url.Action()之后添加它)或创建你自己的扩展方法。

    您的扩展方法可能如下所示:

    public static MvcHtmlString Action(this UrlHelper urlHelper, string action, string controller, string hash)
    {
        return string.Format("{0}#{1}", urlHelper.Action(action, controller), hash);
    }
    

    【讨论】:

    • 牢记这一点很好,毫无疑问 :) 这对于我必须做的事情来说有点太复杂了,最终,我选择了“url-wise”解决方案。
    【解决方案2】:

    您应该使用 LinkExtensions.ActionLink 方法之一。文档可以在这里找到: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx

    【讨论】:

    • 如果您想使用 Html.ActionLink 而不是 Url.Action,这可行。使用此重载并将散列放入片段参数中。 ActionLink(HtmlHelper, 字符串, 字符串, 字符串, 字符串, 字符串, 字符串, 对象, 对象)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2015-02-26
    • 2019-12-24
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多