1+ namespace Microsoft . Web . OData . Routing
2+ {
3+ using Http ;
4+ using Microsoft . OData . Edm ;
5+ using System . Collections . Generic ;
6+ using System . Diagnostics . Contracts ;
7+ using System . Linq ;
8+ using System . Web . Http ;
9+ using System . Web . Http . Controllers ;
10+ using System . Web . OData . Routing ;
11+ using System . Web . OData . Routing . Conventions ;
12+
13+ /// <summary>
14+ /// Represents an OData attribute routing convention with additional support for API versioning.
15+ /// </summary>
16+ public class VersionedAttributeRoutingConvention : AttributeRoutingConvention
17+ {
18+ /// <summary>
19+ /// Initializes a new instance of the <see cref="VersionedAttributeRoutingConvention"/> class.
20+ /// </summary>
21+ /// <param name="model">The <see cref="IEdmModel">EDM model</see> associated with the routing convention.</param>
22+ /// <param name="configuration">The current <see cref="HttpConfiguration">HTTP configuration</see>.</param>
23+ public VersionedAttributeRoutingConvention ( IEdmModel model , HttpConfiguration configuration )
24+ : base ( model , configuration )
25+ {
26+ }
27+
28+ /// <summary>
29+ /// Initializes a new instance of the <see cref="VersionedAttributeRoutingConvention"/> class.
30+ /// </summary>
31+ /// <param name="model">The <see cref="IEdmModel">EDM model</see> associated with the routing convention.</param>
32+ /// <param name="configuration">The current <see cref="HttpConfiguration">HTTP configuration</see>.</param>
33+ /// <param name="pathTemplateHandler">The <see cref="IODataPathTemplateHandler">OData path template handler</see> associated with the routing convention.</param>
34+ public VersionedAttributeRoutingConvention ( IEdmModel model , HttpConfiguration configuration , IODataPathTemplateHandler pathTemplateHandler )
35+ : base ( model , configuration , pathTemplateHandler )
36+ {
37+ }
38+
39+ /// <summary>
40+ /// Initializes a new instance of the <see cref="VersionedAttributeRoutingConvention"/> class.
41+ /// </summary>
42+ /// <param name="model">The <see cref="IEdmModel">EDM model</see> associated with the routing convention.</param>
43+ /// <param name="controllers">The <see cref="IEnumerable{T}">sequence</see> of <see cref="HttpControllerDescriptor">controller descriptors</see>
44+ public VersionedAttributeRoutingConvention ( IEdmModel model , IEnumerable < HttpControllerDescriptor > controllers )
45+ : base ( model , controllers )
46+ {
47+ }
48+
49+ /// <summary>
50+ /// Initializes a new instance of the <see cref="VersionedAttributeRoutingConvention"/> class.
51+ /// </summary>
52+ /// <param name="model">The <see cref="IEdmModel">EDM model</see> associated with the routing convention.</param>
53+ /// <param name="controllers">The <see cref="IEnumerable{T}">sequence</see> of <see cref="HttpControllerDescriptor">controller descriptors</see>
54+ /// associated with the routing convention.</param>
55+ /// <param name="pathTemplateHandler">The <see cref="IODataPathTemplateHandler">OData path template handler</see> associated with the routing convention.</param>
56+ public VersionedAttributeRoutingConvention ( IEdmModel model , IEnumerable < HttpControllerDescriptor > controllers , IODataPathTemplateHandler pathTemplateHandler )
57+ : base ( model , controllers , pathTemplateHandler )
58+ {
59+ }
60+
61+ /// <summary>
62+ /// Returns a value indicating whether the specified controller should be mapped using attribute routing conventions.
63+ /// </summary>
64+ /// <param name="controller">The <see cref="HttpControllerDescriptor">controller descriptor</see> to evaluate.</param>
65+ /// <returns>True if the <paramref name="controller"/> should be mapped as an OData controller; otherwise, false.</returns>
66+ /// <remarks>This method will match any OData controller that is API version-neutral or has a declared API version that
67+ /// matches the API version applied to the associated <see cref="P:Model">model</see>.</remarks>
68+ public override bool ShouldMapController ( HttpControllerDescriptor controller )
69+ {
70+ Contract . Assume ( controller != null ) ;
71+
72+ var versionModel = controller . GetApiVersionModel ( ) ;
73+
74+ if ( versionModel . IsApiVersionNeutral )
75+ {
76+ return true ;
77+ }
78+
79+ var apiVersion = Model . GetAnnotationValue < ApiVersion > ( Model ) ;
80+
81+ return apiVersion != null && versionModel . DeclaredApiVersions . Contains ( apiVersion ) ;
82+ }
83+ }
84+ }
0 commit comments