如何获取请求查询字符串值? [英] How to get Request Querystring values?

查看:263
本文介绍了如何获取请求查询字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API客户端code发送一个认证令牌的查询字符串,如:

  www.example.com/api/user/get/123?auth_token=ABC123

我使用MVC的Web API控制器,​​我有一个过滤器来检查,如果该AUTH_TOKEN有效与否,但我不知道如何访问请求的查询字符串值。

这就是我现在做的,但它显然是错误的:

以下代码段是我的滤波器,可以继承内部:

ActionFilterAttribute

 公共覆盖无效OnActionExecuting(System.Web.Http.Controllers.HttpActionContext ActionContext中)
{
       base.OnActionExecuting(ActionContext中);       如果(actionContext.Request.Properties.ContainsKey(的auth_token)及与放大器;
          actionContext.Request.Properties [AUTH_TOKEN]。的ToString()==ABC123)
       {
         ...
       }
}


解决方案

使用的GetQueryNameValuePairs扩展方法,像这样:

  VAR的queryString = actionContext.Request.GetQueryNameValuePairs()ToDictionary。(X => x.Key,X => x.Value);

My api client code sends an authentication token in the querystring like:

www.example.com/api/user/get/123?auth_token=ABC123

I'm using Mvc Web api controller, and I have a filter that checks if the auth_token is valid or not, but I'm not sure how to access the request querystring values.

This is what I am doing now but it is obviously wrong:

The below snippet is inside of my filter that inherits from:

ActionFilterAttribute

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
       base.OnActionExecuting(actionContext);

       if (actionContext.Request.Properties.ContainsKey("auth_token") &&
          actionContext.Request.Properties["auth_token"].ToString() == "ABC123")
       {
         ...
       }
}

解决方案

Use the GetQueryNameValuePairs extension method, like so:

var queryString = actionContext.Request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value);

这篇关于如何获取请求查询字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆