How can I add an anchor tag to my URL?

前端 未结 1 859
名媛妹妹
名媛妹妹 2020-12-09 17:12

MVC 3.net I want to add an anchor to the end a url.

I tried to include an anchor query string but the hash \'#\' changes to %23 or something like that in the url.

1条回答
  •  囚心锁ツ
    2020-12-09 17:31

    There is an overload of the ActionLink helper that allows you to specify the fragment:

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

    will produce (assuming default routes):

    Link Text
    

    UPDATE:

    and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:

    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);
    }
    

    0 讨论(0)
提交回复
热议问题