繁体   English   中英

ASP.NET MVC - 将GET值附加到每个URL ... ActionLink? 路由? 怎么样?

[英]ASP.NET MVC - append GET value to every URL… ActionLink? Routing? How?

我正在以一种优雅的方式将令牌附加到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包装器? 也许有某种基于路由的解决方案?

可能有一个更优雅的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);
}

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

您可以像这样追加令牌。

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

我建议使用一组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 } ) %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM