數字和大小寫字母都ok,可是中文地址就會有問題web
public bool IslocalURL(string url) { if (string.IsNullOrEmpty(url)) { return false; } Uri absoluteUri; if (Uri.TryCreate(url, UriKind.Absolute, out absoluteUri)) { return String.Equals(this._httpContext.Request.Url.Host, absoluteUri.Host, StringComparison.OrdinalIgnoreCase); } else { bool isLocal = !url.StartsWith("http:", StringComparison.OrdinalIgnoreCase) && !url.StartsWith("https:", StringComparison.OrdinalIgnoreCase) && Uri.IsWellFormedUriString(url, UriKind.Relative); return isLocal; } }
解決辦法:api
利用 Uri.EscapeUriString(String) Methodthis
Converts a URI string to its escaped representation.url
public static string EscapeUriString (string stringToEscape);
.spa
//解決中文url 不會觸發301 pageUrl = Uri.EscapeUriString(pageUrl); //301 (permanent) redirection if (webHelper.IslocalURL(pageUrl)) { filterContext.Result = new RedirectResult(pageUrl, true); }
參考:code
https://docs.microsoft.com/zh-cn/dotnet/api/system.uri?redirectedfrom=MSDN&view=netframework-4.7.2orm