// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Diagnostics.CodeAnalysis; using System.Net.Http.Headers; using System.Web.Http; using System.Web.Http.Routing; namespace System.Net.Http.Formatting { /// /// Class that provides 's from path extension appearing in . /// It uses the value of the {ext} URL parameter from for a match. /// /// /// This sample shows how to use the UriPathExtensionMapping to map urls ending with ".json" to "application/json" /// /// config.Routes.MapHttpRoute("Default", "{controller}"); /// config.Routes.MapHttpRoute("DefaultWithExt", "{controller}.{ext}"); /// config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json"); /// /// public class UriPathExtensionMapping : MediaTypeMapping { public static readonly string UriPathExtensionKey = "ext"; /// /// Initializes a new instance of the class. /// /// The extension corresponding to . /// This value should not include a dot or wildcards. /// The media type that will be returned if is matched. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")] public UriPathExtensionMapping(string uriPathExtension, string mediaType) : base(mediaType) { Initialize(uriPathExtension); } /// /// Initializes a new instance of the class. /// /// The extension corresponding to . /// This value should not include a dot or wildcards. /// The that will be returned if is matched. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")] public UriPathExtensionMapping(string uriPathExtension, MediaTypeHeaderValue mediaType) : base(mediaType) { Initialize(uriPathExtension); } /// /// Gets the path extension. /// [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")] public string UriPathExtension { get; private set; } /// /// Returns a value indicating whether this /// instance can provide a for the given . /// /// The to check. /// If this 's route data contains a match for /// it returns 1.0 otherwise 0.0. public override double TryMatchMediaType(HttpRequestMessage request) { if (request == null) { throw new ArgumentNullException("request"); } string extension = GetUriPathExtensionOrNull(request); return String.Equals(extension, UriPathExtension, StringComparison.OrdinalIgnoreCase) ? 1.0 : 0.0; } private static string GetUriPathExtensionOrNull(HttpRequestMessage request) { IHttpRouteData routeData = request.GetRouteData(); if (routeData != null) { string extension; if (routeData.Values.TryGetValue(UriPathExtensionKey, out extension)) { return extension; } } return null; } private void Initialize(string uriPathExtension) { if (String.IsNullOrWhiteSpace(uriPathExtension)) { throw new ArgumentNullException("uriPathExtension"); } UriPathExtension = uriPathExtension.Trim().TrimStart('.'); } } }