首页 新闻 会员 周边 捐助

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 | 高人七级 | 园豆:30939
提问于: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 | 专家六级 |园豆:14488 | 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 | 园豆:30939 (高人七级) | 2018-07-19 12:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册