1
+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2
+
3
+ namespace Asp . Versioning . Routing ;
4
+
5
+ using System ;
6
+ using System . Collections . Generic ;
7
+ using System . Web . Http . Routing ;
8
+
9
+ internal sealed class WithoutApiVersionUrlHelper : UrlHelper
10
+ {
11
+ private readonly UrlHelper decorated ;
12
+
13
+ public WithoutApiVersionUrlHelper ( UrlHelper decorated ) => this . decorated = decorated ;
14
+
15
+ private ApiVersionRequestProperties Properties => decorated . Request . ApiVersionProperties ( ) ;
16
+
17
+ public override string Content ( string path )
18
+ {
19
+ using ( new NoApiVersionScope ( Properties ) )
20
+ {
21
+ return decorated . Content ( path ) ;
22
+ }
23
+ }
24
+
25
+ public override string Link ( string routeName , object routeValues )
26
+ {
27
+ using ( new NoApiVersionScope ( Properties ) )
28
+ {
29
+ return decorated . Link ( routeName , routeValues ) ;
30
+ }
31
+ }
32
+
33
+ public override string Link ( string routeName , IDictionary < string , object > routeValues )
34
+ {
35
+ using ( new NoApiVersionScope ( Properties ) )
36
+ {
37
+ return decorated . Link ( routeName , routeValues ) ;
38
+ }
39
+ }
40
+
41
+ public override string Route ( string routeName , object routeValues )
42
+ {
43
+ using ( new NoApiVersionScope ( Properties ) )
44
+ {
45
+ return decorated . Route ( routeName , routeValues ) ;
46
+ }
47
+ }
48
+
49
+ public override string Route ( string routeName , IDictionary < string , object > routeValues )
50
+ {
51
+ using ( new NoApiVersionScope ( Properties ) )
52
+ {
53
+ return decorated . Route ( routeName , routeValues ) ;
54
+ }
55
+ }
56
+
57
+ private sealed class NoApiVersionScope : IDisposable
58
+ {
59
+ private readonly ApiVersionRequestProperties properties ;
60
+ private readonly string ? rawVersion ;
61
+ private readonly ApiVersion ? version ;
62
+ private bool disposed ;
63
+
64
+ public NoApiVersionScope ( ApiVersionRequestProperties properties )
65
+ {
66
+ this . properties = properties ;
67
+ rawVersion = properties . RawRequestedApiVersion ;
68
+ version = properties . RequestedApiVersion ;
69
+ properties . RawRequestedApiVersion = default ;
70
+ properties . RequestedApiVersion = default ;
71
+ }
72
+
73
+ public void Dispose ( )
74
+ {
75
+ if ( disposed )
76
+ {
77
+ return ;
78
+ }
79
+
80
+ disposed = true ;
81
+ properties . RawRequestedApiVersion = rawVersion ;
82
+ properties . RequestedApiVersion = version ;
83
+ }
84
+ }
85
+ }
0 commit comments