【问题标题】:ASP.NET MVC - append GET value to every URL... ActionLink? Routing? How?ASP.NET MVC - 将 GET 值附加到每个 URL ... ActionLink?路由?如何?
【发布时间】:2010-11-23 00:32:35
【问题描述】:

我正在寻找一种优雅的方式来将令牌附加到 ASP.NET MVC 应用程序中的每个 URL。例如:

http://mysite.com/?token=81541858

从任何给定的页面生成链接时(例如通过 HTML.ActionLink),它应该将现有标记附加到新 URL。例如:

HTML.ActionLink("Show me","Detail",new{id=5})

应该产生: http://mysite.com/Product/Detail/5?token=81541858

在保持现有整体设计的同时实现这一目标的最佳方式是什么。 ActionLink 包装器?也许有某种基于路由的解决方案?

【问题讨论】:

    标签: asp.net-mvc routing http-headers session-variables token


    【解决方案1】:

    可能有更优雅的 MVC/路由解决方案,但简单的扩展方法应该可以解决问题:

    public static string TokenActionLink(this HtmlHelper html, 
                                         string linkText, 
                                         string actionName, 
                                         string controllerName, 
                                         int id, 
                                         int token)
    {
       var anchorFormat = "<a href=\"{0}\">{1}</a>";
       var urlFormat = "{0}/{1}/{2}?token={3}";
       return string.Format(anchorFormat, string.Format(urlFormat, controllerName, actionName, id, token.ToString()), linkText);
    }
    

    用法

    <%: Html.TokenActionLink("Show Me", "Detail", "Product", Model.Id, Model.Token) %>
    

    或者,也许您可​​以创建一个自定义 RouteValueDictionary:,并从您的自定义方法中调用常规 ActionLink 方法:

    public static string TokenActionLink(this HtmlHelper html, 
                                             string linkText, 
                                             string actionName, 
                                             string controllerName, 
                                             int id, 
                                             int token)
    {
         var rvd = new RouteValueDictionary(ViewContext.RouteData.Values);
         rvd["Token"] = token.ToString();
         return Html.ActionLink(linkText, actionName, controllerName, id, rvd);
    }
    

    【讨论】:

      【解决方案2】:

      HtmlHelper 上创建一个自定义的TokenActionLink 扩展方法,在其中从查询字符串中获取当前的token 值并将其附加到链接查询字符串。您可以拥有与普通 ActionLink 相同的重载,并且 token 键的 append..ation 是透明的

      public static MvcHtmlString TokenActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
      {
          var token = htmlHelper.ViewContext.RequestContext.HttpContext.Request.QueryString["token"];
          var routeValuesDict = new RouteValueDictionary(routeValues);
          routeValuesDict["token"] = token;
          return htmlHelper.ActionLink(linkText, actionName, routeValuesDict);
      }
      

      【讨论】:

        【解决方案3】:

        我建议使用一组 HtmlHelper 扩展,它们使用令牌的源并调用实际的 HtmlHelper 扩展,将令牌添加到 RouteValueDictionary。不过,您需要确保在视图中使用扩展。

        public static class HtmlHelperExtensions
        {
        
            public static string TokenActionLink( this HtmlHelper helper, string text, string action, object routeValues )
            {
                  var token = GetToken(helper);
        
                  var values = new RouteValueDictionary();
                  if (routeValues != null)
                  {
                      values = new RouteValueDictionary( routeValues );
                  }
                  if (!values.ContainsKey( "token" ))
                  {
                      values.Add("token", token );
                  }
        
                  return helper.ActionLink( action, values );
            }
        
            ... other custom helpers for different signatures...
        
            private static string GetToken( HtmlHelper helper )
            {
                ...get token from session or request parameters...
            }
        }
        

        用作:

        <%= Html.TokenActionLink( "action", new { id = 5 } ) %>
        

        【讨论】:

        • 很多类似的答案基本上都回答了相同的问题,但这是公认的注重细节的答案。特别是“(!values.ContainsKey(“token”))”。简单的事情......
        【解决方案4】:

        您可以像这样附加令牌。

        HTML.ActionLink("Show me","Detail",new{id=5, token=myTokenVariable})
        

        【讨论】:

        • 我认为目标是让它自动化,这样人们就不必记住总是在创建链接的代码中添加令牌。
        • @tvanfosson...问题不是那么具体。我只是想提供 OP 可能不知道的 ActionLink Helper 的详细信息。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-11
        • 1970-01-01
        • 2012-04-20
        相关资源
        最近更新 更多