如何在我的网址中添加锚标记?

Dev*_*ave 19 tags url anchor add asp.net-mvc-3

MVC 3.net我想在网址末尾添加一个锚点.

我试图包含一个锚点查询字符串,但是哈希"#"更改为%23或类似于url中的内容.

有办法解决这个问题吗?

Dar*_*rov 36

ActionLink助手有一个重载,允许您指定片段:

@Html.ActionLink(
    "Link Text",           // linkText
    "Action",              // actionName
    "Controller",          // controllerName
    null,                  // protocol
    null,                  // hostName
    "fragment",            // fragment
    new { id = "123" },    // routeValues
    null                   // htmlAttributes
)
Run Code Online (Sandbox Code Playgroud)

将产生(假设默认路线):

<a href="/Controller/Action/123#fragment">Link Text</a>
Run Code Online (Sandbox Code Playgroud)

更新:

如果你想在执行重定向的控制器操作中执行此操作,可以使用GenerateUrl方法:

public ActionResult Index()
{
    var url = UrlHelper.GenerateUrl(
        null,
        "Action",
        "Controller",
        null,
        null,
        "fragment",
        new RouteValueDictionary(new { id = "123" }),
        Url.RouteCollection,
        Url.RequestContext,
        false
    );
    return Redirect(url);
}
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,您可以在控制器中使用[UrlHelper.GenerateUrl](http://msdn.microsoft.com/en-us/library/ee703653.aspx)方法,该方法允许您指定片段,然后重定向到结果网址.我已经更新了我的帖子以提供一个例子. (4认同)