《ASP.NET Core 知识速递 – Day 3:每天进步一点》 文章中我们讲解了如何更优雅的方式创建编码的url查询字符串,在这节中我们我们使用System.Net.Http.FormUrlEncodedContent类对name/values进行编码并生成一个查询字符串的url,使用的application/x-www-form-urlencoded MIME 类型。
使用ASP.NET Core Empty的项目模板创建一个空的项目:
var app = WebApplication.Create();
app.Run(async context =>
{
var dicts = new Dictionary()
{
[“id”] = “001”,
[“name”] = “gui bingbing”,
[“birthday”] = “1986/08/30”,
[“guid”] = Guid.NewGuid().ToString(),
[“artist”] = “Bill Gui”,
[“formula”] = “10 * 5 = 50”
};
using var queryString = new FormUrlEncodedContent(dicts);
context.Response.Headers.Append(“Content-Type”, “text/html;charset=utf-8”);
await context.Response.WriteAsync($@”
使用 FormUrlEncodedContent 创建编码的URL字符串
输入 “); await context.Response.WriteAsync(“
- {k.Key} = {k.Value}
“); await context.Response.WriteAsync(“输出
“); await context.Response.WriteAsync(await queryString.ReadAsStringAsync()); await context.Response.WriteAsync(“”); await context.Response.WriteAsync(@””);
});
app.Run();
运行应用程序:
源代码地址:
https://github.com/bingbing-gui/AspNetCore-Skill/tree/master/src/aspnetcore-knowledge-point/form-url-encoded-content
声明:文中观点不代表本站立场。本文传送门:http://eyangzhen.com/421901.html