asp.net-mvc-3 - 如何将 anchor 标记添加到我的 URL?

标签 asp.net-mvc-3 url tags anchor add

MVC 3.net 我想在 url 末尾添加一个 anchor 。

我试图包含一个 anchor 查询字符串,但哈希 '#' 在 url 中更改为 %23 或类似的东西。

有没有办法解决这个问题?

最佳答案

ActionLink 过载允许您指定片段的助手:

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

将产生(假设默认路由):
<a href="/Controller/Action/123#fragment">Link Text</a>

更新:

如果您想在执行重定向的 Controller 操作中执行此操作,您可以使用 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);
}

关于asp.net-mvc-3 - 如何将 anchor 标记添加到我的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904835/

相关文章:

mysql - 亚马逊网络服务 + ASP.NET MVC 3 + MySql

javascript - codeigniter 无法在单独的 js 文件中回显 site_url

php - 标签数据库设计

javascript - 脚本标签和页面有效性的最低要求

asp.net-mvc - MVC3 十进制在编辑时被截断为 2 位小数

c# - 在 asp.net mvc 3 中管理每个 session 和请求的 AutoFac 生命周期范围

ASP.Net SQL 连接字符串配置

javascript - 在点后添加空格但不在 URL 中添加空格

url - URL 中保留的分号有何用途?

python - 当每个标签向文本添加样式时,处理文本小部件中的多个标签吗?