首页新闻找找看学习计划

C# 字符串转换问题

0
悬赏园豆:30 [已解决问题] 解决于 2018-07-19 12:08

在 .NET Core 中如何简单高效地完成下面的字符串转换?

var originRedirectUri = "https://q.cnblogs.com/account/signin?ReturnUrl=%2F";

转换为

var redirectUri = "https://passport.cnblogs.com/account/signin?ReturnUrl=https%3A%2F%2Fq.cnblogs.com%2F`";
C#
dudu的主页 dudu | 高人七级 | 园豆:38628
提问于:2018-07-19 10:37
< >
分享
最佳答案
0

UriBuilder u = new UriBuilder("url");
var q = System.Web.HttpUtility.ParseQueryString(u.Query);
q["ReturnUrl"] = "url";
u.Query = q.ToString();
var url = u.ToString();

收获园豆:30
czd890 | 大侠五级 |园豆:8412 | 2018-07-19 11:10

在 ASP.NET Core 中使用 Uri + UriBuilder + QueryHelpers 实现了,实现代码如下:

private string BuildAbsoluteRedirectUri(string redirectUri, HttpRequest request)
{
    var originUri = new Uri(redirectUri);
    var uriBuilder = new UriBuilder("https", "passport.cnblogs.com");
    uriBuilder.Path = originUri.AbsolutePath;
    var queryStrings = QueryHelpers.ParseQuery(originUri.Query);
    var returnUrl = originUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) + queryStrings["ReturnUrl"];
    uriBuilder.Query = QueryString.Create("ReturnUrl", returnUrl).ToString();
    return uriBuilder.ToString();
}
dudu | 园豆:38628 (高人七级) | 2018-07-19 12:08
其他回答(1)
0

Regex.Replace("","")????????????????????????????????????????????????????????????

不会Redis | 园豆:455 (菜鸟二级) | 2018-07-19 14:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册