Repository navigation
Expand file tree
/
Copy pathLocalizedRoute.cs
More file actions
184 lines (157 loc) · 7.47 KB
/
Copy pathLocalizedRoute.cs
File metadata and controls
184 lines (157 loc) · 7.47 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Localization;
using SmartStore.Core.Infrastructure;
namespace SmartStore.Web.Framework.Localization
{
/// <summary>
/// Provides properties and methods for defining a localized route, and for getting information about the localized route.
/// </summary>
public class LocalizedRoute : Route, ICloneable<LocalizedRoute>
{
#region Fields
private bool? _seoFriendlyUrlsForLanguagesEnabled;
private string _leftPart;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the System.Web.Routing.Route class, using the specified URL pattern and handler class.
/// </summary>
/// <param name="url">The URL pattern for the route.</param>
/// <param name="routeHandler">The object that processes requests for the route.</param>
public LocalizedRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
/// <summary>
/// Initializes a new instance of the System.Web.Routing.Route class, using the specified URL pattern, handler class and default parameter values.
/// </summary>
/// <param name="url">The URL pattern for the route.</param>
/// <param name="defaults">The values to use if the URL does not contain all the parameters.</param>
/// <param name="routeHandler">The object that processes requests for the route.</param>
public LocalizedRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(url, defaults, routeHandler)
{
}
/// <summary>
/// Initializes a new instance of the System.Web.Routing.Route class, using the specified URL pattern, handler class, default parameter values and constraints.
/// </summary>
/// <param name="url">The URL pattern for the route.</param>
/// <param name="defaults">The values to use if the URL does not contain all the parameters.</param>
/// <param name="constraints">A regular expression that specifies valid values for a URL parameter.</param>
/// <param name="routeHandler">The object that processes requests for the route.</param>
public LocalizedRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
: base(url, defaults, constraints, routeHandler)
{
}
/// <summary>
/// Initializes a new instance of the System.Web.Routing.Route class, using the specified URL pattern, handler class, default parameter values,
/// constraints,and custom values.
/// </summary>
/// <param name="url">The URL pattern for the route.</param>
/// <param name="defaults">The values to use if the URL does not contain all the parameters.</param>
/// <param name="constraints">A regular expression that specifies valid values for a URL parameter.</param>
/// <param name="dataTokens">Custom values that are passed to the route handler, but which are not used to determine whether the route matches a specific URL pattern. The route handler might need these values to process the request.</param>
/// <param name="routeHandler">The object that processes requests for the route.</param>
public LocalizedRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
: base(url, defaults, constraints, dataTokens, routeHandler)
{
}
#endregion
#region Methods
/// <summary>
/// Returns information about the requested route.
/// </summary>
/// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
/// <returns>
/// An object that contains the values from the route definition.
/// </returns>
public override RouteData GetRouteData(HttpContextBase httpContext)
{
if (DataSettings.DatabaseIsInstalled() && this.SeoFriendlyUrlsForLanguagesEnabled)
{
var helper = new LocalizedUrlHelper(httpContext.Request);
if (helper.IsLocalizedUrl())
{
helper.StripSeoCode();
httpContext.RewritePath("~/" + helper.RelativePath, true);
}
}
if (_leftPart == null)
{
var url = this.Url;
int idx = url.IndexOf('{');
_leftPart = "~/" + (idx >= 0 ? url.Substring(0, idx) : url).TrimEnd('/');
}
// Perf
if (!httpContext.Request.AppRelativeCurrentExecutionFilePath.StartsWith(_leftPart, true, CultureInfo.InvariantCulture))
return null;
RouteData data = base.GetRouteData(httpContext);
return data;
}
/// <summary>
/// Returns information about the URL that is associated with the route.
/// </summary>
/// <param name="requestContext">An object that encapsulates information about the requested route.</param>
/// <param name="values">An object that contains the parameters for a route.</param>
/// <returns>
/// An object that contains information about the URL that is associated with the route.
/// </returns>
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData data = base.GetVirtualPath(requestContext, values);
if (data != null && DataSettings.DatabaseIsInstalled() && this.SeoFriendlyUrlsForLanguagesEnabled)
{
var helper = new LocalizedUrlHelper(requestContext.HttpContext.Request, true);
string cultureCode;
if (helper.IsLocalizedUrl(out cultureCode))
{
if (!requestContext.RouteData.Values.ContainsKey("StripInvalidSeoCode"))
{
data.VirtualPath = String.Concat(cultureCode, "/", data.VirtualPath);
}
}
}
return data;
}
public virtual void ClearSeoFriendlyUrlsCachedValue()
{
_seoFriendlyUrlsForLanguagesEnabled = null;
}
#endregion
#region Properties
public bool IsClone { get; private set; }
protected bool SeoFriendlyUrlsForLanguagesEnabled
{
get
{
if (!_seoFriendlyUrlsForLanguagesEnabled.HasValue)
_seoFriendlyUrlsForLanguagesEnabled = EngineContext.Current.Resolve<LocalizationSettings>().SeoFriendlyUrlsForLanguagesEnabled;
return _seoFriendlyUrlsForLanguagesEnabled.Value;
}
}
#endregion
#region Clone Members
public LocalizedRoute Clone()
{
var clone = new LocalizedRoute(this.Url,
new RouteValueDictionary(this.Defaults),
new RouteValueDictionary(this.Constraints),
new RouteValueDictionary(this.DataTokens),
new MvcRouteHandler());
clone.RouteExistingFiles = this.RouteExistingFiles;
clone._seoFriendlyUrlsForLanguagesEnabled = this._seoFriendlyUrlsForLanguagesEnabled;
clone.IsClone = true;
return clone;
}
object ICloneable.Clone()
{
return this.Clone();
}
#endregion
}
}