[英]How to set a query string value in C# relative urls
Assume there is a relative URL like: /api/services
or /api/services?id=4
.假设有一个亲戚 URL 像:
/api/services
或/api/services?id=4
。 What is the best practice to add another query string parameter to the specified URL. For example, adding type=1
should lead to the following results: /api/services?type=1
and /api/services?id=4&type-1
.将另一个查询字符串参数添加到指定的 URL 的最佳做法是什么。例如,添加
type=1
应导致以下结果: /api/services?type=1
和/api/services?id=4&type-1
。 The type of the URLs is general, and any part of the standard URI format may exist. URL 的类型是通用的,标准 URI 格式的任何部分都可能存在。
Note that the standard System.Uri
and System.UriBuilder
classes have limited capability for relative URIs.请注意,标准
System.Uri
和System.UriBuilder
类对相对 URI 的功能有限。
Add nuget package Microsoft.AspNetCore.WebUtilities
.添加 nuget package
Microsoft.AspNetCore.WebUtilities
。
namespace so74768078
{
internal class Program
{
static void Main(string[] args)
{
var test1 = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString("/api/services", "type", "1");
var test2 = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString("/api/services?id=4", "type", "1");
Console.WriteLine(test1);
Console.WriteLine(test2);
}
}
}
console output控制台 output
/api/services?type=1
/api/services?id=4&type=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.