首页 > 解决方案 > 如何将 url 参数保留为键值

问题描述

通常我可以使用 httpcontext 读取参数 inurl 但我想知道 url 中是否有多个查询参数我可以将它们作为键值获取吗?

url: localhost:3034/?CustomerID=12345&RoleName=Student

string customerID = this.HttpContext.Request["CustomerID"];
string roleName= this.HttpContext.Request["RoleName"];

我是否有机会将它作为键值来保存,而不是像上面那样一一阅读并在项目的任何地方使用它?

提前致谢

标签: c#httpcontexturl-parameters

解决方案


您可以直接访问 Response 对象的 Headers 属性以获取标头或 QueryString 属性以获取查询参数。他们都已经是一个集合:

NameValueCollection headers = this.HttpContext.Request.Headers;
NameValueCollection queryParams = this.HttpContext.Request.QueryString;

推荐阅读