7

如果您一直在寻找一种很好且干净的方法来解析查询字符串值,我想出了这个:

    /// <summary>
    /// Parses the query string and returns a valid value.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key">The query string key.</param>
    /// <param name="value">The value.</param>
    protected internal T ParseQueryStringValue<T>(string key, string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            //TODO: Map other common QueryString parameters type ...
            if (typeof(T) == typeof(string))
            {
                return (T)Convert.ChangeType(value, typeof(T));
            }
            if (typeof(T) == typeof(int))
            {
                int tempValue;
                if (!int.TryParse(value, out tempValue))
                {
                    throw new ApplicationException(string.Format("Invalid QueryString parameter {0}. The value " +
                                                              "'{1}' is not a valid {2} type.", key, value, "int"));
                }
                return (T)Convert.ChangeType(tempValue, typeof(T));
            }
            if (typeof(T) == typeof(DateTime))
            {
                DateTime tempValue;
                if (!DateTime.TryParse(value, out tempValue))
                {
                    throw new ApplicationException(string.Format("Invalid QueryString parameter {0}. The value " +
                                                         "'{1}' is not a valid {2} type.", key, value, "DateTime"));
                }
                return (T)Convert.ChangeType(tempValue, typeof(T));
            }
        }
        return default(T);
    }

我一直想拥有这样的东西,最后做对了……至少我是这么认为的……

代码应该是不言自明的......

任何使它更好的意见或建议表示赞赏。

4

7 回答 7

34

一种简单的解析方法(如果您不想进行类型转换)是

 HttpUtility.ParseQueryString(queryString);

您可以从 URL 中提取查询字符串

 new Uri(url).Query
于 2009-09-03T00:13:38.060 回答
5

鉴于您只处理三种不同的类型,我建议改为使用三种不同的方法 - 泛型方法最好适用于类型约束允许的每个类型参数。

此外,我强烈建议您指定要使用的int文化DateTime- 它不应该真正取决于服务器恰好所处的文化。(如果您有代码来猜测用户的文化,您可以使用它相反。)最后,我还建议支持一组明确指定的DateTime格式,而不仅仅是TryParse默认支持的任何格式。(我几乎总是使用ParseExact/TryParseExact而不是Parse/ TryParse。)

请注意,字符串版本实际上不需要做任何事情,因为它value已经是一个字符串(尽管您当前的代码将 "" 转换为null,这可能是您想要的,也可能不是您想要的)。

于 2009-02-22T12:51:58.703 回答
3

我编写了以下方法来将 QueryString 解析为强类型值:

public static bool TryGetValue<T>(string key, out T value, IFormatProvider provider)
{
    string queryStringValue = HttpContext.Current.Request.QueryString[key];

    if (queryStringValue != null)
    {
        // Value is found, try to change the type
        try
        {
            value = (T)Convert.ChangeType(queryStringValue, typeof(T), provider);
            return true;
        }
        catch
        {
            // Type could not be changed
        }
    }

    // Value is not found, return default
    value = default(T);
    return false;
}

使用示例:

int productId = 0;
bool success = TryGetValue<int>("ProductId", out productId, CultureInfo.CurrentCulture);

对于 的查询字符串?productId=5将为bool真并且int productId等于 5。

对于 的查询字符串?productId=hellobool是假的,int productId将等于 0。

对于 的查询字符串?noProductId=notIncludedbool是假的,int productId将等于 0。

于 2009-12-17T11:39:55.037 回答
2

在我的应用程序中,我一直在使用以下功能:-

public static class WebUtil
{
    public static T GetValue<T>(string key, StateBag stateBag, T defaultValue)
    {
        object o = stateBag[key];

        return o == null ? defaultValue : (T)o;
    }
}

如果未提供参数,则返回所需的默认值,从 defaultValue 推断类型,并根据需要引发强制转换异常。

用法如下:-

var foo = WebUtil.GetValue("foo", ViewState, default(int?));
于 2009-02-22T13:02:04.823 回答
2

这是一个旧答案,但我做了以下事情:

            string queryString = relayState.Split("?").ElementAt(1);
            NameValueCollection nvc = HttpUtility.ParseQueryString(queryString);
于 2012-09-04T22:16:20.960 回答
1

在我看来,您正在做很多不必要的类型转换。tempValue 变量是您尝试返回的类型的前导。同样在字符串的情况下,值已经是一个字符串,所以只需返回它。

于 2009-02-22T12:54:08.603 回答
0

根据Ronalds 的回答,我更新了自己的查询字符串解析方法。我使用它的方法是将它作为扩展方法添加到 Page 对象上,这样我就可以轻松检查查询字符串的值和类型,并在页面请求无效时重定向。

扩展方法如下所示:

public static class PageHelpers
{
    public static void RequireOrPermanentRedirect<T>(this System.Web.UI.Page page, string QueryStringKey, string RedirectUrl)
    {
        string QueryStringValue = page.Request.QueryString[QueryStringKey];

        if(String.IsNullOrEmpty(QueryStringValue))
        {
            page.Response.RedirectPermanent(RedirectUrl);
        }

        try
        {
            T value = (T)Convert.ChangeType(QueryStringValue, typeof(T));
        }
        catch
        {
            page.Response.RedirectPermanent(RedirectUrl);
        }
    }
}

这让我可以执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    Page.RequireOrPermanentRedirect<int>("CategoryId", "/");
}

然后,我可以编写其余代码并依赖查询字符串项的存在和正确格式,因此我不必在每次想要访问它时都对其进行测试。

注意:如果您使用的是 .net 4 之前的版本,那么您还需要以下 RedirectPermanent 扩展方法:

public static class HttpResponseHelpers
{
    public static void RedirectPermanent(this System.Web.HttpResponse response, string uri)
    {
        response.StatusCode = 301;
        response.StatusDescription = "Moved Permanently";
        response.AddHeader("Location", uri);
        response.End();
    }
}
于 2011-05-06T13:43:41.637 回答