将新查询字符串添加到 C# 中的现有 URL 并重定向到它

问题描述 投票:0回答:1

任何人都可以帮助我使用 C# 代码将两个查询字符串添加到现有 URL。 URL 通常如下所示。当在 C# 中创建 URL 时,也会重定向到它。非常感谢任何帮助。

http://localhost/somesite/index.php?Filename=somefile.txt&Filepath=E:\myfolder
c# html .net url
1个回答
0
投票

建立网址将是 选项A:(字符串连接)


string param1 = "Filename=somefile.txt";
    string param2 = @"Filepath=E:\myfolder";
    Uri YourURL = new Uri($"http://localhost/somesite/index.php?{param1}&{param2}");
    Console.WriteLine(YourURL);

选项b:(URIBuilder)

UriBuilder uriBuilder = new UriBuilder();
    uriBuilder.Scheme = "http";
    uriBuilder.Host = "localhost";
    uriBuilder.Path = "somesite/index.php";
    //uriBuilder.Port = 80;
    
    var q = new Dictionary<string,string>();
    q.Add("Filename","somefile.txt");
    q.Add("Filepath","E:\\myfolder");
    uriBuilder.Query = string.Join("&", q.Select(s => $"{s.Key}={s.Value}").ToList());
    uriBuilder.ToString().Dump();

重定向。不确定您使用什么 UI。这个问题很模糊。 但要重定向试试这个:

Response.Redirect(YourURL);

否则,请用更好的解释重写你的问题

© www.soinside.com 2019 - 2024. All rights reserved.