93

こんにちは私はMvcコントローラーからアンカーを返したいです

コントローラ名=DefaultController;

public ActionResult MyAction(int id)
{
        return RedirectToAction("Index", "region")
}

インデックスに向けられたときのURLが

http://localhost/Default/#region

となることによって

<a href=#region>the content should be focus here</a>

私はあなたがこのようにそれをすることができるかどうか尋ねていません:どうすれば私のURLにアンカータグを追加できますか?

4

4 に答える 4

139

私はこの方法を見つけました:

public ActionResult MyAction(int id)
{
    return new RedirectResult(Url.Action("Index") + "#region");
}

この冗長な方法を使用することもできます。

var url = UrlHelper.GenerateUrl(
    null,
    "Index",
    "DefaultController",
    null,
    null,
    "region",
    null,
    null,
    Url.RequestContext,
    false
);
return Redirect(url);

http://msdn.microsoft.com/en-us/library/ee703653.aspx

于 2012-05-21T18:22:29.000 に答える
16

素晴らしい答えグドロン。これが私が使用する別の方法です(ここで利用可能なソリューションに追加するだけです)。

return Redirect(String.Format("{0}#{1}", Url.RouteUrl(new { controller = "MyController", action = "Index" }), "anchor_hash");

明らかに、gdoron の答えを使用すると、この単純なケースでは次のようにしてよりクリーンにすることができます。

return new RedirectResult(Url.Action("Index") + "#anchor_hash");
于 2014-01-29T03:49:24.640 に答える
4

Squall の回答を拡張するには: 文字列補間を使用すると、よりクリーンなコードになります。また、さまざまなコントローラーでのアクションに対しても機能します。

return Redirect($"{Url.RouteUrl(new { controller = "MyController", action = "Index" })}#anchor");
于 2016-10-31T10:51:54.500 に答える