2

当没有为请求指定路径时,这是做永久重定向的正确方法吗?当没有指定路径时,ASP.NET Core permament重定向

 app.Use(next => context => 
     { 
      if (string.IsNullOrWhiteSpace(context.Request.Path)) 
      { 
       var builder = new UriBuilder(context.Request.Scheme, "site to redirect"); 
       context.Response.Redirect(builder.ToString(), true); 
      } 
      return next(context); 
     }); 

更新1

似乎context.Request.Path包括/

 app.Use(next => context => 
     { 
      if (context.Request.Path.Value.Length <= 1) 
      { 
       var builder = new UriBuilder(context.Request.Scheme, "www.plaMobi.com"); 
       context.Response.Redirect(builder.ToString(), true); 
      } 
      return next(context); 
     }); 

回答

0

因此到UriHelper实施方式中,既HttpRequest.PathBase ABD HttpRequest.Path应使用:

var combinedPath = (pathBase.HasValue || path.HasValue) 
        ? (pathBase + path).ToString() : "/"; 

ProxyMiddleware类中的相同逻辑:

var uriString = $"{_options.Scheme}://{_options.Host}:{_options.Port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";