Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        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());
        }