Ejemplo n.º 1
0
        public void ShouldRewriteTest(AppSettings appSettings, string requestUrl, bool expectedShould, string expectedUrl)
        {
            bool should = _helper.ShouldRewrite(appSettings, requestUrl, out string url);

            Assert.Equal(expectedShould, should);
            Assert.Equal(expectedUrl, url);
        }
        public Task Invoke(HttpContext context, IHttpWwwRewriter helper)
        {
            // has to locate service instead of inject in for appsettings update to be picked up in middleware automatically
            var settings = context.RequestServices.GetService <IOptionsSnapshot <AppSettings> >().Value;

            _logger.LogDebug("PreferredDomain {@PreferredDomain}", settings.PreferredDomain);
            _logger.LogDebug("UseHttps {@UseHttps}", settings.UseHttps);

            if (helper.ShouldRewrite(settings, context.Request.GetDisplayUrl(), out string url))
            {
                _logger.LogInformation("RewriteUrl: {@RewriteUrl}", url);

                context.Response.Headers[HeaderNames.Location] = url;
                context.Response.StatusCode = 301;
                context.Response.Redirect(url, permanent: true);
                return(Task.CompletedTask);
            }

            return(_next(context));
        }