Пример #1
0
        public override RouteData GetRouteData(HttpContextBase context)
        {
            var result = _route.GetRouteData(context);

            if (result == null)
            {
                var url       = _session.GetUrlFromRequest(context.Request);
                var languages = context.Request.UserLanguages ?? new[] { I18N.DefaultTwoLetterISOLanguageName };
                foreach (var language in languages.Where(language => !string.IsNullOrWhiteSpace(language)))
                {
                    var semiColonIndex = language.IndexOf(';');
                    var token          = string.Format("/{0}", semiColonIndex > -1 ? language.Substring(0, semiColonIndex) : language);
                    if (!url.EndsWithAnyIgnoreCase(token, token + "/"))
                    {
                        continue;
                    }

                    url = url.TrimEnd(token.Substring(1).ToCharArray());
                    var originalRequest = new ClonedHttpRequest(context.Request, url);
                    var originalContext = new ClonedHttpContext(context, originalRequest);

                    result = _route.GetRouteData(originalContext);
                    if (result != null)
                    {
                        // Found the original non-decorated route
                        return(result);
                    }
                }
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Called before an action method executes.
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.HttpContext.Request;
            var values  = filterContext.RouteData.Values;

            // Value is already injected from a route declaration
            if (values.ContainsKey("language"))
            {
                return;
            }

            // Value is part of the explicit route and is the preferred language (from those available)
            var preferred = _session.GetLanguageFromSessionOrService(filterContext.HttpContext);
            var url       = _session.GetUrlFromRequest(filterContext.HttpContext.Request);

            if (url.EndsWithAnyIgnoreCase(string.Format("/{0}", preferred), string.Format("/{0}/", preferred)))
            {
                return;
            }

            // Value is loose in the query string, i.e. '/?language=en'
            if (request.QueryString["language"] != null)
            {
                _session.Set(filterContext.HttpContext, request.QueryString["language"]);
                preferred = _session.GetLanguageFromSessionOrService(filterContext.HttpContext);
            }

            // Value is part of the explicit route, i.e. '/about/en' but not the preferred language
            var languages = request.UserLanguages ?? new[] { I18N.DefaultTwoLetterISOLanguageName };

            foreach (var language in languages.Where(language => !string.IsNullOrWhiteSpace(language)))
            {
                var semiColonIndex = language.IndexOf(';');
                var token          = string.Format("/{0}", semiColonIndex > -1 ? language.Substring(0, semiColonIndex) : language);
                if (!url.EndsWithAnyIgnoreCase(token, string.Format("{0}/", token)))
                {
                    continue;
                }

                // This is an explicit language request, override preferences
                _session.Set(filterContext.HttpContext, token.Substring(1));
                preferred = _session.GetLanguageFromSessionOrService(filterContext.HttpContext);
            }

            RedirectWithLanguage(filterContext, values, preferred);
        }