【问题标题】:ASP.NET Core 2 Url Rewriting middleware to redirect from .xxx to .yyy extensionASP.NET Core 2 Url 重写中间件以从 .xxx 重定向到 .yyy 扩展名
【发布时间】:2018-03-19 17:42:18
【问题描述】:

我想将所有请求从 .com 重定向到 .net,并在 dot net core 中使用相同的路径和路由。

与以下代码相同,用于从起始 URL 中删除 www:

app.UseRewriter(new RewriteOptions().Add(ctx =>
{
    // checking if the hostName has www. at the beginning
    var req = ctx.HttpContext.Request;
    var hostName = req.Host;
    if (hostName.ToString().StartsWith("www."))
    {
        // Strip off www.
        var newHostName = hostName.ToString().Substring(4);

        // Creating new url
        var newUrl = new StringBuilder()
                              .Append(req.Scheme)
                              .Append(newHostName)
                              .Append(req.PathBase)
                              .Append(req.Path)
                              .Append(req.QueryString)
                              .ToString();

        // Modify Http Response
        var response = ctx.HttpContext.Response;
        response.Headers[HeaderNames.Location] = newUrl;
        response.StatusCode = 301;
        ctx.Result = RuleResult.EndResponse;
    }
}));

【问题讨论】:

    标签: asp.net-core-2.0 rewriting


    【解决方案1】:

    您可以更改现有代码以将 com 更改为 net。

    if (hostName.ToString().EndsWith(".com"))
    {
        // change the com to net
        var newHostName = hostName.ToString().Substring(0, hostName.ToString().Length - 4) + ".net";
    
        // Creating new url
        :
        :
    }
    

    我没有测试过,但应该可以。

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多