简体   繁体   English

在ASP.NET MVC中查询Querystring

[英]Concating Querystring in ASP.NET MVC

I'm using ASP.NET MVC for the first time and so far everything has been going great. 我第一次使用ASP.NET MVC,到目前为止一切都很顺利。 However, one little question I was hoping someone could advise me on. 然而,一个小问题我希望有人可以告诉我。

When a page can perform multiple tasks and each is handled on the server by assessing querystring values what is the recommended solutions for building up the querystring over requests. 当页面可以执行多个任务并且每个任务都通过评估查询字符串值在服务器上处理时,建议查询字符串的建议解决方案是什么。

For example, a list of products the user may sort, page and filter. 例如,用户可以对其进行排序,分页和过滤的产品列表。

The user performs a sort - ?sortcolumn=example 用户执行排序 - ?sortcolumn =示例

The user changes page on the list - ?sortcolumn=example&page=3 用户更改列表中的页面 - ?sortcolumn = example&page = 3

The user performs a filter on the list - ?sortcolumn=example&page=3&filter=expr 用户在列表上执行过滤器 - ?sortcolumn = example&page = 3&filter = expr

The Actionlinks for each of these tasks is totally seperate from the others, so I need the routevalues in all of the Html.ActionLink expressions. 每个任务的Actionlinks与其他任务完全分开,因此我需要所有Html.ActionLink表达式中的routevalues。

Hoping somone can advise. 希望somone可以提供建议。

Thanks for the help guys. 谢谢你的帮助。 I ended up creating an extention method on the UrlHelper to easily build urls that require all active url/querystring data to be passed back. 我最终在UrlHelper上创建了一个扩展方法,以便轻松构建需要传回所有活动url / querystring数据的URL。 I done this because using the stringbuilder would have been way to difficult to start altering urls based on matches in the routing table. 我这样做是因为使用stringbuilder会很难根据路由表中的匹配开始更改URL。

for example /players/defender/4 or players?sortcolumn=defender&page=4 例如/ players / defender / 4或者player?sortcolumn = defender&page = 4

Using this method I can just put everything back into the routing engine and let it worry about which parts of the url are to be mapped to the url, which parts to the querystring. 使用这种方法,我可以将所有内容放回到路由引擎中,让它担心url的哪些部分要映射到url,哪些部分是querystring。

using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class UrlHelperExtensions
    {
        public static string RouteUrlIncludingQuerystring(this UrlHelper helper)
        {
            return helper.RouteUrlIncludingQuerystring(new RouteValueDictionary());
        }

        public static string RouteUrlIncludingQuerystring(this UrlHelper helper, object routeValues)
        {
            return helper.RouteUrlIncludingQuerystring(new RouteValueDictionary(routeValues));
        }

        public static string RouteUrlIncludingQuerystring(this UrlHelper helper, RouteValueDictionary routeValues)
        {
            RouteValueDictionary v = new RouteValueDictionary();

            foreach (var kvp in helper.RequestContext.RouteData.Values)
                v[kvp.Key] = kvp.Value;

            foreach (var kvp in helper.RequestContext.RouteData.DataTokens)
                v[kvp.Key] = kvp.Value;

            foreach (var key in helper.RequestContext.HttpContext.Request.QueryString.AllKeys)
                v[key] = helper.RequestContext.HttpContext.Request.QueryString[key];

            foreach (var kvp in routeValues)
                v[kvp.Key] = kvp.Value;

            return helper.RouteUrl(v);
        }
    }
}

You can create a single route that hits a particular controller method, and just add parameters to the controller method as needed. 您可以创建一个命中特定控制器方法的路径,并根据需要将参数添加到控制器方法中。 If the parameter exists in the querystring, the controller method will pick it up. 如果参数存在于查询字符串中,则控制器方法将获取它。 If the parameter does not exist in the querystring, the parameter in the controller method will simply be null. 如果查询字符串中不存在该参数,则控制器方法中的参数将为空。

Example: 例:

    public ActionResult ProductsBySystemIndex(int manufacturerID, string page, string sortOrder, string filter)
    {
         if (page != null)
         {
             // do something with page
         }
         // etc.
    }

Take a look at the Pager helper from MvcContrib here The approach they use is calling a helper method for link creation that will iterate through the current querystring keys and append the necessary values for page navigation (ignoring the current page but preserving the other params). 这里看一下MvcContrib的Pager帮助器他们使用的方法是调用一个辅助方法来创建链接,它将迭代当前的查询字符串键并为页面导航附加必要的值(忽略当前页面但保留其他参数)。 The sample is very basic since it only works for paging but could be easily extended to include sorting and filtering. 该示例非常基础,因为它仅适用于分页,但可以很容易地扩展到包括排序和过滤。

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

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