// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Specialized; using System.Net.Http.Headers; using System.Web.Http; namespace System.Net.Http.Formatting { /// /// Class that provides s from query strings. /// public class QueryStringMapping : MediaTypeMapping { private static readonly Type _queryStringMappingType = typeof(QueryStringMapping); /// /// Initializes a new instance of the class. /// /// The name of the query string parameter to match, if present. /// The value of the query string parameter specified by . /// The media type to use if the query parameter specified by is present /// and assigned the value specified by . public QueryStringMapping(string queryStringParameterName, string queryStringParameterValue, string mediaType) : base(mediaType) { Initialize(queryStringParameterName, queryStringParameterValue); } /// /// Initializes a new instance of the class. /// /// The name of the query string parameter to match, if present. /// The value of the query string parameter specified by . /// The to use if the query parameter specified by is present /// and assigned the value specified by . public QueryStringMapping(string queryStringParameterName, string queryStringParameterValue, MediaTypeHeaderValue mediaType) : base(mediaType) { Initialize(queryStringParameterName, queryStringParameterValue); } /// /// Gets the query string parameter name. /// public string QueryStringParameterName { get; private set; } /// /// Gets the query string parameter value. /// public string QueryStringParameterValue { get; private set; } /// /// Returns a value indicating whether the current /// instance can return a from . /// /// The to check. /// If this instance can produce a from /// it returns 1.0 otherwise 0.0. public override double TryMatchMediaType(HttpRequestMessage request) { if (request == null) { throw Error.ArgumentNull("request"); } NameValueCollection queryString = GetQueryString(request.RequestUri); return DoesQueryStringMatch(queryString) ? FormattingUtilities.Match : FormattingUtilities.NoMatch; } private static NameValueCollection GetQueryString(Uri uri) { if (uri == null) { throw Error.InvalidOperation(Properties.Resources.NonNullUriRequiredForMediaTypeMapping, _queryStringMappingType.Name); } return new FormDataCollection(uri).ReadAsNameValueCollection(); } private void Initialize(string queryStringParameterName, string queryStringParameterValue) { if (String.IsNullOrWhiteSpace(queryStringParameterName)) { throw Error.ArgumentNull("queryStringParameterName"); } if (String.IsNullOrWhiteSpace(queryStringParameterValue)) { throw Error.ArgumentNull("queryStringParameterValue"); } QueryStringParameterName = queryStringParameterName.Trim(); QueryStringParameterValue = queryStringParameterValue.Trim(); } private bool DoesQueryStringMatch(NameValueCollection queryString) { if (queryString != null) { foreach (string queryParameter in queryString.AllKeys) { if (String.Equals(queryParameter, QueryStringParameterName, StringComparison.OrdinalIgnoreCase)) { string queryValue = queryString[queryParameter]; if (String.Equals(queryValue, QueryStringParameterValue, StringComparison.OrdinalIgnoreCase)) { return true; } } } } return false; } } }