Repository navigation
Expand file tree
/
Copy pathLanguageSeoCodeAttribute.cs
More file actions
113 lines (97 loc) · 4.65 KB
/
Copy pathLanguageSeoCodeAttribute.cs
File metadata and controls
113 lines (97 loc) · 4.65 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
using System;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SmartStore.Core;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Localization;
using SmartStore.Core.Infrastructure;
using SmartStore.Services.Localization;
using SmartStore.Web.Framework.Localization;
namespace SmartStore.Web.Framework.Controllers
{
/// <summary>
/// Attribute which ensures that store URL contains a language SEO code if "SEO friendly URLs with multiple languages" setting is enabled
/// </summary>
public class LanguageSeoCodeAttribute : ActionFilterAttribute
{
public Lazy<IWorkContext> WorkContext { get; set; }
public Lazy<ILanguageService> LanguageService { get; set; }
public Lazy<LocalizationSettings> LocalizationSettings { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null || filterContext.HttpContext == null)
return;
HttpRequestBase request = filterContext.HttpContext.Request;
if (request == null)
return;
//don't apply filter to child methods
if (filterContext.IsChildAction)
return;
//only GET requests
if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
return;
// ensure that this route is registered and localizable (LocalizedRoute in RouteProvider.cs)
if (filterContext.RouteData == null || filterContext.RouteData.Route == null || !(filterContext.RouteData.Route is LocalizedRoute))
return;
if (!DataSettings.DatabaseIsInstalled())
return;
var localizationSettings = LocalizationSettings.Value;
if (!localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
return;
// process current URL
var workContext = WorkContext.Value;
var languageService = LanguageService.Value;
var workingLanguage = workContext.WorkingLanguage;
var helper = new LocalizedUrlHelper(filterContext.HttpContext.Request, true);
string defaultSeoCode = languageService.GetDefaultLanguageSeoCode();
string seoCode;
if (helper.IsLocalizedUrl(out seoCode))
{
if (!languageService.IsPublishedLanguage(seoCode))
{
var descriptor = filterContext.ActionDescriptor;
// language is not defined in system or not assigned to store
if (localizationSettings.InvalidLanguageRedirectBehaviour == InvalidLanguageRedirectBehaviour.ReturnHttp404)
{
filterContext.Result = new ViewResult
{
ViewName = "NotFound",
MasterName = (string)null,
ViewData = new ViewDataDictionary<HandleErrorInfo>(new HandleErrorInfo(new HttpException(404, "The resource does not exist."), descriptor.ActionName, descriptor.ControllerDescriptor.ControllerName)),
TempData = filterContext.Controller.TempData
};
filterContext.RouteData.Values["StripInvalidSeoCode"] = true;
filterContext.RequestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
filterContext.RequestContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
else if (localizationSettings.InvalidLanguageRedirectBehaviour == InvalidLanguageRedirectBehaviour.FallbackToWorkingLanguage)
{
helper.StripSeoCode();
filterContext.Result = new RedirectResult(helper.GetAbsolutePath(), true);
}
}
else
{
// redirect default language (if desired)
if (seoCode == defaultSeoCode && localizationSettings.DefaultLanguageRedirectBehaviour == DefaultLanguageRedirectBehaviour.StripSeoCode)
{
helper.StripSeoCode();
filterContext.Result = new RedirectResult(helper.GetAbsolutePath(), true);
}
}
// already localized URL, skip the rest
return;
}
// keep default language prefixless (if desired)
if (workingLanguage.UniqueSeoCode == defaultSeoCode && (int)(localizationSettings.DefaultLanguageRedirectBehaviour) > 0)
{
return;
}
// add language code to URL
helper.PrependSeoCode(workingLanguage.UniqueSeoCode);
filterContext.Result = new RedirectResult(helper.GetAbsolutePath());
}
}
}