且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用ASP.NET Core从查询字符串中读取值?

更新时间:2023-02-17 08:47:08

你可以在 IQueryCollection 上使用ToString方法,如果指定了一个 page 参数,它将返回所需的值:

You could use the ToString method on IQueryCollection which will return the desired value if a single page parameter is specified:

string page = HttpContext.Request.Query["page"].ToString();

如果有多个值,例如?page = 1& page = 2 然后ToString调用的结果将是 1,2

if there are multiple values like ?page=1&page=2 then the result of the ToString call will be 1,2

但是作为@mike -g在他的回答中建议你***使用模型绑定而不是直接访问 HttpContext.Request.Query 对象。

But as @mike-g suggested in his answer you would better use model binding and not directly accessing the HttpContext.Request.Query object.