/// <summary> /// Appends a culture cookie to the HTTP response, when appropriate, to store the client's culture preference /// </summary> /// <remarks> /// <para>If the current request does not already contain a culture cookie, a new one will be appended to the response.</para> /// <para>If the request contains a culture cookie but it is different from the current culture, a new cookie will be appended.</para> /// </remarks> public Task Invoke(HttpContext context) { var request = context.Request; var path = request.Path; var excludedRoutes = _routeCultureOptions.Value.ExcludedRoutes ?? new List <string>(); if (ExcludedRouteProvider.IsExcludedRoute(excludedRoutes, path)) { return(_next(context)); } var locale = CultureInfo.CurrentCulture.Name; var cookie = context.Request.Cookies[CookieRequestCultureProvider.DefaultCookieName]; var cookieValue = CookieRequestCultureProvider.ParseCookieValue(cookie); if (!string.IsNullOrWhiteSpace(cookie) && cookieValue.Cultures.Contains(locale)) { return(_next(context)); } context.Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture: locale, uiCulture: locale)), new CookieOptions { Expires = _cookieExpires, IsEssential = _cookieIsEssential } ); _logger.LogLocalizationCookieAppended(request.Path); return(_next(context)); }
/// <summary> /// Tests the request culture against supported application cultures and non-culture values. Throws a <see cref="CultureNotFoundException"/> if request culture is not supported. /// </summary> public Task Invoke(HttpContext context) { var path = context.Request.Path; var parameters = context.Request.Path.Value.Split('/'); if (parameters is null || parameters.Length <= 1) { return(_next(context)); } var culture = parameters[_routeCultureOptions.Value.CultureParameterIndex]; var excludedRoutes = _routeCultureOptions.Value.ExcludedRoutes ?? new List <string>(); if (ExcludedRouteProvider.IsExcludedRoute(excludedRoutes, path)) { return(_next(context)); } else if (IsSupportedCulture(culture)) { return(_next(context)); } else { throw new CultureNotFoundException(); } }