【问题标题】:How to add a parameter to the current URL in a view?如何向视图中的当前 URL 添加参数?
【发布时间】:2014-02-21 11:04:17
【问题描述】:

我有一个基于 ASP.NET MVC4 的 Web 应用程序,其中我使用包含三个语言标志(图像)的布局。这些标志在整个站点中都是可见的。现在我想使用这些图像进行语言选择。为了以特定语言显示任何页面,我只需向当前 URL 添加一个参数并加载它,然后我的控制器对其进行评估并在模型中设置正确的语言(或者在有请求中未指定语言)。

这是一个例子:

未指定语言:

http://example.com?StationId=12

指定语言:

http://example.com?StationId=12&lang=1033

我的问题是:如何在我的布局中形成一个围绕语言标志图像的链接,并始终指向当前加载的 URL,所有参数 加上一个新参数 &lang=1033 分别@ 987654324@万一只有语言参数?

换句话说:如何使用所有参数创建指向同一页面的链接,并简单地将路由值添加到 URL?

更新:这里有一个详细的例子

在我的_Layout.cshtml 我有:

@{
var u_eng = new RouteValueDictionary(Url.RequestContext.RouteData.Values);
var u_deu = new RouteValueDictionary(Url.RequestContext.RouteData.Values);
var u_por = new RouteValueDictionary(Url.RequestContext.RouteData.Values);
u_eng.Add("lang", "1033");
u_deu.Add("lang", "1031");
u_por.Add("lang", "1046");
}
.
.
.
<li>
    <a href="@Url.RouteUrl(u_por)" class="Flag BR">BR</a>
</li>
<li>
    <a href="@Url.RouteUrl(u_eng)" class="Flag US">US</a>
</li>
<li>
    <a href="@Url.RouteUrl(u_deu)" class="Flag DE">DE</a>
</li>

现在,当我运行网站时,起始页上的链接工作正常,它们指向当前页面并将 lang=xy 参数添加到相应的 URL。

但是当我转到另一个使用相同布局文件并使用参数的页面时,链接无法正确构建:假设我转到由该 URL 定义的子页面:

http://localhost:1234/Stations?position=up

页面本身会正确显示。但是当我悬停或点击语言链接时,它们指向:

http://localhost:1234/Stations?lang=1031 而不是

http://localhost:1234/Stations?position=up&amp;lang=1031

如您所见,所有现有参数都被省略了。

【问题讨论】:

    标签: c# asp.net-mvc-4 razor hyperlink routevalues


    【解决方案1】:

    您可以使用当前路由值集合,为其添加新值,然后使用 url helper RouteUrl 生成 url。要收集传递给页面的查询字符串参数,您可以遍历 HttpContext.Request.QueryString 字典:

    @{
        var currentRouteValues = new RouteValueDictionary(Url.RequestContext.RouteData.Values);
        var queryString = Request.QueryString;
        foreach (var key in queryString.AllKeys)
        {
            currentRouteValues.Add(key, queryString[key]);
        }
    
        currentRouteValues.Add("lang", "1033"); 
    }
    

    然后像这样使用它:

    @Url.RouteUrl(currentRouteValues)
    

    【讨论】:

    • 这正是我想要的。谢谢!
    • 糟糕......不幸的是,它没有按预期工作:我将上面的代码构建到我的布局文件中,在其中我使用 @Url.RouteUrl(currentRouteValues) 作为“a”标签中的 href ,但似乎它只构建一次,然后它不会“识别”任何子页面上的任何现有路由值,只有“lang”参数被设置。
    • @Robert,你能把代码贴出来吗?我不确定我是否理解问题
    • @Robert,谢谢,现在我明白了。查询字符串参数需单独检索,请看更新
    【解决方案2】:

    我稍微扩展了@Andrei 的答案,并创建了辅助方法来在视图中构建Urls。

    这些是辅助方法:

            private static RouteValueDictionary GetKeyValuePairs(this UrlHelper urlHelper)
            {
                var routeValueDictionary = new RouteValueDictionary(urlHelper.RequestContext.RouteData.Values);
                var queryString = HttpContext.Current.Request.QueryString;
                foreach (var key in queryString.AllKeys) routeValueDictionary.Add(key, queryString[key]);
    
                return routeValueDictionary;
            }
    
    
            public static string BuildUrl(this UrlHelper urlHelper)
            {
                var routeValueDictionary = GetKeyValuePairs(urlHelper);
                return urlHelper.RouteUrl(routeValueDictionary);
            }
    
    
            public static string BuildUrl(this UrlHelper urlHelper, string key)
            {
                var routeValueDictionary = GetKeyValuePairs(urlHelper);
    
                if (routeValueDictionary.Keys.Contains(key)) routeValueDictionary.Remove(key);
    
                return urlHelper.RouteUrl(routeValueDictionary);
            }
    
    
            public static string BuildUrl(this UrlHelper urlHelper, string key, object value)
            {
                var routeValueDictionary = GetKeyValuePairs(urlHelper);
                if (routeValueDictionary.ContainsKey(key)) routeValueDictionary[key] = value;
                else routeValueDictionary.Add(key, value);
    
                return urlHelper.RouteUrl(routeValueDictionary);
    
            }
    

    那么在我看来,我是这样使用的:

    <a href='@Url.BuilUrl()' >Preserve current query string.</a>
    <a href='@Url.BuilUrl("param2", "value")' >Add item to query string.</a>
    <a href='@Url.BuilUrl("param1")' >Remove item from query string.</a>
    

    http://localhost:5000/?param2=value2 的结果:

    <a href='http://localhost:5000/?param2=value2'>Preserve current query string.</a>
    <a href='http://localhost:5000/?param2=value2&param1=value'>Add item to query string.</a>
    <a href='http://localhost:5000/?param1=value' >Remove item from query string.</a>
    

    希望对大家有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2014-11-28
      • 2015-02-21
      相关资源
      最近更新 更多