【问题标题】:ASP.NET Core permament redirect when no path specified未指定路径时 ASP.NET Core 永久重定向
【发布时间】:2017-02-09 11:55:12
【问题描述】:

当没有为请求指定路径时,这是进行永久重定向的正确方法吗?

        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);
        });

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc url-redirection http-redirect response.redirect


    【解决方案1】:

    根据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}";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 2012-02-24
      • 1970-01-01
      • 2014-01-12
      • 2013-02-08
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多