2

요청에 지정된 경로가 없을 때 영구 리디렉션을 수행하는 올바른 방법입니까?ASP.NET 경로가 지정되지 않은 경우 핵심 유효 기간 리디렉션

 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.PathUriHelper 구현 /

 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

따라서 포함 나타나는 두 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}";