forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriPathExtensionMapping.cs
More file actions
103 lines (91 loc) · 4.74 KB
/
Copy pathUriPathExtensionMapping.cs
File metadata and controls
103 lines (91 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using System.Web.Http.Routing;
namespace System.Net.Http.Formatting
{
/// <summary>
/// Class that provides <see cref="MediaTypeHeaderValue"/>'s from path extension appearing in <see cref="IHttpRouteData"/>.
/// It uses the value of the {ext} URL parameter from <see cref="IHttpRouteData"/> for a match.
/// </summary>
/// <example>
/// This sample shows how to use the UriPathExtensionMapping to map urls ending with ".json" to "application/json"
/// <code>
/// config.Routes.MapHttpRoute("Default", "{controller}");
/// config.Routes.MapHttpRoute("DefaultWithExt", "{controller}.{ext}");
/// config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");
/// </code>
/// </example>
public class UriPathExtensionMapping : MediaTypeMapping
{
public static readonly string UriPathExtensionKey = "ext";
/// <summary>
/// Initializes a new instance of the <see cref="UriPathExtensionMapping"/> class.
/// </summary>
/// <param name="uriPathExtension">The extension corresponding to <paramref name="mediaType"/>.
/// This value should not include a dot or wildcards.</param>
/// <param name="mediaType">The media type that will be returned if <paramref name="uriPathExtension"/> is matched.</param>
[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);
}
/// <summary>
/// Initializes a new instance of the <see cref="UriPathExtensionMapping"/> class.
/// </summary>
/// <param name="uriPathExtension">The extension corresponding to <paramref name="mediaType"/>.
/// This value should not include a dot or wildcards.</param>
/// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> that will be returned if <paramref name="uriPathExtension"/> is matched.</param>
[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);
}
/// <summary>
/// Gets the <see cref="Uri"/> path extension.
/// </summary>
[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; }
/// <summary>
/// Returns a value indicating whether this <see cref="UriPathExtensionMapping"/>
/// instance can provide a <see cref="MediaTypeHeaderValue"/> for the given <paramref name="request"/>.
/// </summary>
/// <param name="request">The <see cref="HttpRequestMessage"/> to check.</param>
/// <returns>If this <paramref name="request"/>'s route data contains a match for <see cref="UriPathExtension"/>
/// it returns <c>1.0</c> otherwise <c>0.0</c>.</returns>
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('.');
}
}
}