HttpRequest.RouteValues属性不可从代码访问,但可从调试器访问 [英] HttpRequest.RouteValues property is not accessible from code but accessible from debugger
问题描述
我正在尝试创建对特定请求进行一些检查的中间件.
I am trying to create middleware which performs some checking for particular requests.
例如,我有这样的路线:
For example, I have such routes:
-
api/Test/{paramToCheck}/aaa
-
api/Test/bbb/ccc
api/Test/{paramToCheck}/aaa
api/Test/bbb/ccc
我有这些要求:
-
http://some-host-and-port/api/Test/1234/aaa
-
http://some-host-and-port/api/Test/bbb/ccc
http://some-host-and-port/api/Test/1234/aaa
http://some-host-and-port/api/Test/bbb/ccc
现在在我的中间件内部,我想检查请求是否包含{paramToCheck}
并获取此参数的值.
Now inside my middleware I want to check if request contains {paramToCheck}
and get value of this parameter.
当我在InvokeAsync
中设置断点时,我可以看到httpContext.Request.RouteValues
属性包含所有需要的数据(Keys
包含"paramToCheck"
,而Values
包含其值).
When I set breakpoint inside InvokeAsync
I can see httpContext.Request.RouteValues
property containing all needed data (Keys
contains "paramToCheck"
and Values
contains its value).
但是在代码中我无法访问此属性,但出现错误:
But in code I can't access this property, I get error:
错误CS1061:"HttpRequest"不包含以下内容的定义: "RouteValues",并且没有可访问的扩展方法"RouteValues" 可以找到接受类型为'HttpRequest'的第一个参数 您缺少using指令或程序集引用吗?)
Error CS1061: 'HttpRequest' does not contain a definition for 'RouteValues' and no accessible extension method 'RouteValues' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)
var value = httpContext.Request.RouteValues["paramToCheck"];
如何访问此属性或如何执行所需的检查?
How can I access this property or how can I perform needed check?
代码:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware
(
RequestDelegate next
) => this._next = next;
public async Task InvokeAsync(HttpContext httpContext)
{
var value = httpContext.Request.RouteValues["paramToCheck"]; // error here
//... some logis with value
await this._next(httpContext);
}
}
编辑
中间件位于netstandard2.1
类库中,不能将其移动到api项目中,因为它很常见,应该由多个api项目使用.
Middleware is inside netstandard2.1
class library and it cannot be moved to api project as it is common and should be used by several api projects.
更新
似乎当前无法实现,因为在NetCoreApp 3
元包中的Microsoft.AspNetCore.Http.Abstractions 3.0.0
中添加了RouteValues
属性.由于最新版本为2.2.0
,因此无法在netstandard 2.1
项目中安装此软件包.
Seems like currently it cannot be achieved as RouteValues
propery was added in Microsoft.AspNetCore.Http.Abstractions 3.0.0
which is inside NetCoreApp 3
metapackage. And there is no possibility to install this package in netstandard 2.1
project because the latest version is 2.2.0
.
推荐答案
听起来像您尝试从无法访问httpContext的服务实现中访问它一样,我的意思是说要在api项目之外使用它.
sounds like you trying to access this from a service implementation which does not have access to httpContext, am i correct in saying to have this outside of your api project.
如果您在controller中执行httpContext.Request.RouteValues["paramToCheck"];
可以正常工作...然后使其工作,请在controller中执行此操作并将结果传递给lib.
if you do httpContext.Request.RouteValues["paramToCheck"];
in controller does it works... then to get it to work , do this in controller and pass the result to the lib.
httpContext具有与BaseController或ControllerBase的绑定" *…无论调用什么..基本上,您期望的连接都没有在lib中完成...因此httpContext根本没有Web上下文,有一个更好的方式来表达这一点……全部,但是您得到了公正.
httpContext has "binding"* to BaseController or ControllerBase... what ever its call.. basically the wiring which you are expecting is not done in your lib... so httpContext has no web context at all, there is a much better way to word this... all, but you get the just.
这篇关于HttpRequest.RouteValues属性不可从代码访问,但可从调试器访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!