Owin 获取查询字符串参数 [英] Owin get query string parameters
问题描述
我正在尝试从 Owin 请求中获取查询字符串参数.尽管此参数在查询字符串中,但参数test"的获取操作仍为空.如何从 OWIN 主机读取请求参数?
I am trying to get query string parameters from Owin request. Get operation for parameter 'test' remains empty, although this parameter was in query string. How can I read request parameter from OWIN host?
调用:
localhost:5000/?test=firsttest
代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHandlerAsync((req, res) =>
{
string paramTest = req.Get<string>("test");
return res.WriteAsync(paramTest);
});
}
推荐答案
Get
在 OWIN 环境字典中查找任何键.但是,单个 GET 请求参数不是该字典的一部分.您可以使用 req.QueryString
获取完整的查询字符串,它等效于 req.Get
并返回 test=firsttest
在你的情况下.这很容易解析.
Get<T>
looks in the OWIN environment dictionary for any key. The individual GET request parameters aren't part of that dictionary though. You could get the complete query string using req.QueryString
which is equivalent to req.Get<string>("owin.RequestQueryString")
and returns test=firsttest
in your case. That could be easily parsed.
另一种选择是这样的:
app.Use(async (ctx, next) =>
{
var param = ctx.Request.Query.Get("test");
await next();
});
IOwinRequest
实现为您提供解析的查询字符串.请注意,从 IOwinContext.Request
获取的对象实现了 IOwinRequest
而传递给 UseHandlerAsync
的对象是一种完全不同的类型(IOwinRequest
>Owin.Types.OwinRequest) 既不提供上下文也不提供解析的查询字符串 (afaik).
IOwinRequest
implementations provide you with a parsed query string. Note that the object one gets from IOwinContext.Request
implements IOwinRequest
while the object that is passed to UseHandlerAsync
is of a completely different type (Owin.Types.OwinRequest
) which neither provides the context nor the parsed query string (afaik).
这篇关于Owin 获取查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!