HttpRequest.RouteValues property is not accessible from code but accessible from debugger

穿精又带淫゛_ 提交于 2020-05-14 20:00:12

问题


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

and I have these requests:

  • http://some-host-and-port/api/Test/1234/aaa
  • http://some-host-and-port/api/Test/bbb/ccc

Now inside my middleware I want to check if request contains {paramToCheck} and get value of this parameter.

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:

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?

Code:

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);
    }
}

EDIT

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.

UPDATE

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.


回答1:


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.

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 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.




回答2:


Just encountered the same problem today.

This is hacky but it works

IReadOnlyDictionary<string, object>? routeValues = ((dynamic)context.Request).RouteValues as IReadOnlyDictionary<string, object>;



回答3:


public async Task InvokeAsync(HttpContext httpContext)
{
    //Instead of this
    var value = httpContext.Request.RouteValues["paramToCheck"];
   //Use like this
    string originalPath = context.Request.Path.ToString();
   //You will get your desired values to check your validation
   //and then
  //just check like this
 if (originalPath.Trim().Contains("api/URL/{paramToCheck}", 
     StringComparison.InvariantCultureIgnoreCase)
 //then do your necessary validation here



回答4:


You can use the extension .GetRouteData() from RoutingHttpContextExtensions which will provide you a RouteData instance. This extension is part of Microsoft.AspNetCore.Routing package.

The RouteData contains a property member of type RouteValueDictionary called Values.

var routeData = httpContext.GetRouteData();

if (routeData.Values.TryGetValue("paramToCheck", out object value))
{
    // ... Do some logic with the value
}

To understand a bit more about the RoutingHttpContextExtensions and RouteData check the source code at https://github.com/dotnet/aspnetcore/tree/3.0/src/Http/Routing



来源:https://stackoverflow.com/questions/59610509/httprequest-routevalues-property-is-not-accessible-from-code-but-accessible-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!