Beispiel #1
0
        private ICultureExpression ExtractLanguageFromUrl(HttpContext context, ICultureContext cultureContext, out string urlSpecifier, out string action)
        {
            ICultureOption cultureOption = cultureContext.CultureOption;
            string         path          = context.Request.Path.Value;

            if (UrlComponents.TryParse(path, out UrlComponents urlComponents))
            {
                action = urlComponents.Action;
                context.Request.Path = new PathString(action);
                urlSpecifier         = urlComponents.CultureSpecifier ?? "";
                if (urlSpecifier.Length > 0)
                {
                    context.Request.PathBase = context.Request.PathBase.Add(new PathString(urlSpecifier));
                }

                if (urlComponents.Culture != null && checkCultureSupported && !cultureContext.CultureOption.IsCultureSupported(urlComponents.Culture))
                {
                    return(null);
                }
                return(urlComponents.Culture);
            }
            else
            {
                urlSpecifier = "";
                action       = path;
                return(null);
            }
        }
Beispiel #2
0
        public Task Invoke(HttpContext context, ICultureContext cultureContext)
        {
            if (filter == null)
            {
                InvokeMiddleware(context, cultureContext);
            }
            else
            {
                UrlFilterResult filterResult = filter(context.Request.Path);
                if (filterResult is InvokeMiddlewareFilterResult)
                {
                    InvokeMiddleware(context, cultureContext);
                }
                else if (filterResult is SetCultureFilterResult setCultureResult)
                {
                    cultureContext.UrlCultureSpecifier = setCultureResult.CultureSpecifier;
                    cultureContext.CurrentCulture      = setCultureResult.Culture;
                    cultureContext.Action    = setCultureResult.Action;
                    context.Request.Path     = new PathString(cultureContext.Action);
                    context.Request.PathBase = new PathString(setCultureResult.PathBase);
                }
                else if (filterResult is CheckHeaderFilterResult)
                {
                    cultureContext.UrlCultureSpecifier = "";
                    ICultureExpression cultureExpression = ExtractLanguageFromHeader(context, cultureContext);
                    if (cultureExpression == null)
                    {
                        cultureExpression = cultureContext.CultureOption.DefaultCulture;
                    }
                    cultureContext.Action         = context.Request.Path;
                    cultureContext.CurrentCulture = cultureExpression;
                }
                else
                {
                    ICultureExpression cultureExpression = cultureContext.CultureOption.DefaultCulture;
                    cultureContext.Action         = context.Request.Path;
                    cultureContext.CurrentCulture = cultureExpression;
                    return(next(context));
                }
            }

            string urlSpecifier = cultureContext.UrlCultureSpecifier;

            if (urlSpecifier.Length <= 0)
            {
                return(next(context));
            }
            else
            {
                return(next(context).ContinueWith(tsk =>
                {
                    if ((context.Response.StatusCode == 301 || context.Response.StatusCode == 302) &&
                        context.Response.Headers.TryGetValue("Location", out StringValues locationValue) &&
                        locationValue.Count > 0)
                    {
                        string location = locationValue[0];
                        if (UrlComponents.TryParse(location, out UrlComponents urlComponents))
                        {
                            if (urlComponents.IsRelative)
                            {
                                if (!string.IsNullOrEmpty(urlComponents.CultureSpecifier))
                                {
                                    // dose not change the location
                                    // context.Response.Headers["Location"] = urlSpecifier + context.Response.Headers["Location"];
                                    return;
                                }
                                else
                                {
                                    urlComponents.TrySetCulture(urlSpecifier);
                                    context.Response.Headers["Location"] = urlComponents.FullActionWithQuery;
                                    return;
                                }
                            }
                            else
                            {
                                string host = context.Request.Host.Value;
                                string requestedHost = urlComponents.FullHost;
                                if (requestedHost != null && requestedHost.Equals(host, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    if (!string.IsNullOrEmpty(urlComponents.CultureSpecifier))
                                    {
                                        // dose not change the location
                                        // context.Response.Headers["Location"] = urlSpecifier + context.Response.Headers["Location"];
                                        return;
                                    }
                                    else
                                    {
                                        urlComponents.TrySetCulture(urlSpecifier);
                                        context.Response.Headers["Location"] = urlComponents.FullActionWithQuery;
                                        return;
                                    }
                                }
                                else
                                {
                                    // dose not change the location
                                    // context.Response.Headers["Location"] = urlSpecifier + context.Response.Headers["Location"];
                                    return;
                                }
                            }
                        }
                        else
                        {
                            // dose not change the location
                            // context.Response.Headers["Location"] = urlSpecifier + context.Response.Headers["Location"];
                        }
                    }
                }));
            }
        }