どのように私はRedirectToRouteResultにアンカー/ハッシュのためのパラメータを追加しますか?

StackOverflow https://stackoverflow.com/questions/1796177

  •  22-09-2019
  •  | 
  •  

質問

私は/ 4#の概要/ユーザのようなURLにリダイレクトするRedirectToRouteResultを使用したいです。 ASP.NET MVC 1.0を使用して、私はそれを行うための方法を見つけることができていない - ?私は

それを見逃しています
役に立ちましたか?

解決

あなたは適切にルーティングテーブルであなたのルートを構築する必要があります。例:ます。

routes.MapRoute("MyRoute" ,"{controler}/{id}#{detail}" ,new { controller = "users", action = "index", id = (string)null, detail = "Summary" });

他のヒント

UrlHelper.GenerateUrlフラグメントパラメータを含みます。私は、拡張メソッドを作成しました。

        public static string Action(this UrlHelper url, string actionName, string controllerName, string fragment, object routeValues)
        {
            return UrlHelper.GenerateUrl(
                routeName: null,
                actionName: actionName,
                controllerName: controllerName,
                routeValues: new System.Web.Routing.RouteValueDictionary(routeValues),
                fragment: fragment,
                protocol: null,
                hostName: null,
                routeCollection: url.RouteCollection,
                requestContext: url.RequestContext,
                includeImplicitMvcValues: true /*helps fill in the nulls above*/
            );
        }

それから私はRedirectToFragmentResultクラスを作成しました。

public class RedirectToFragmentResult: RedirectResult
{
    public UrlHelper Url {get;set;}
    public string Action { get; set; }
    public string Controller { get; set; }
    public string Fragment { get; set; }
    public object RouteValues { get; set; }
    public RedirectToFragmentResult(UrlHelper url, string action, string controller, string fragment, object routeValues)
        :base(url.Action(action, controller, fragment, routeValues))
    {
        Url = url;
        Action = action;
        Controller = controller;
        Fragment = fragment;
        RouteValues = routeValues;
    }
}

次に、あなたはそれにあなたがRedirectToRouteResultの場合と方法を確認するためにあなたのユニットテストで新しいRouteValueDictionary(result.RouteValues)を作成することができます。

私は同様の問題があったが、ここで見てみます:

(ページ上の場所へのリンク# ASP.NET MVCのメカニズムを介してID)か

私は#を使用してルートを作成してしまっています。

私は私のサイトここの上でそのような何かを行います。しかし、それはRedirectToRouteResultではありません。 RedirectToRouteResultがURLにアンカー部を含むサポートしていません。

あなたはアンカー部の処理を扱うために、リンク自身とおそらくロジックを構築する必要がある - 私が行ったように。私のアプリケーションはFacebookのフォトギャラリービューと同様の複製機能をしようとします。私はアンカー部を使用し、このために、異なるページへの各リンクは、固有のURLを持っている必要があります。しかし、それは私が適切なコンテンツをロードするページと使用AJAXに手動でURLのアンカー部分を解析する必要がルートへの直接変換されませんだって。これは私のために働くので、私が望んでいたものです。

scroll top