![](/img/trans.png)
[英]Analyzer exceptions after migrating asp.net core 2.2 project to version 3.1
[英]How to force all URLs to lowercase in ASP.NET Core 2.2 and newer version?
我想將所有 URL 轉換為小寫。
例如:
https://ABC123.com/
-> https://abc123.com/
https://StackoverFlow.com/
-> https://stackoverflow.com/
提前致謝
我添加了一個實現 IRule 的復雜重定向,例如:
公共 class RedirectLowerCaseRule: IRule { public int StatusCode { get; } = (int)HttpStatusCode.MovedPermanently;
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
PathString path = context.HttpContext.Request.Path;
HostString host = context.HttpContext.Request.Host;
if (path.HasValue && path.Value.Any(char.IsUpper) || host.HasValue && host.Value.Any(char.IsUpper))
{
HttpResponse response = context.HttpContext.Response;
response.StatusCode = StatusCode;
response.Headers[HeaderNames.Location] = (request.Scheme + "://" + host.Value + request.PathBase.Value + request.Path.Value).ToLower() + request.QueryString;
context.Result = RuleResult.EndResponse;
}
else
{
context.Result = RuleResult.ContinueRules;
}
}
}
然后可以將其應用到您的 Startup.cs 下的 Configure 方法中:
新的 RewriteOptions().Add(新的 RedirectLowerCaseRule());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.