Create a HttpRequest instance in .Net Core 3.1

I have to create a HttpRequest instance with GET verb, and Uri, to pass the object to OdataQueryOptions.

Below is the sample code in c#. I need to convert in Dotnet core 3.1.

[EnableQuery]    public class MyEnableQueryAttribute : EnableQueryAttribute     {         public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)         {             //IQueryable result = default(IQueryable);              // get the original request before the alterations             var originalRequest = queryOptions.Request.QueryString;              // get the original URL before the alterations             string url = originalRequest.Value;              if (queryOptions.Filter != null && url.Contains("$filter=indexof%28Type,%27PRIMARY%27%29%20ne%20-1"))             {                 url = url.Replace("$filter=indexof%28Type,%27PRIMARY%27%29%20ne%20-1", "$filter=indexof%28Type,%27toupper%28PRIMARY%28%27%29%20ne%20-1");                 //var context = new HttpContext()                 //HttpRequestMessageFeature request = new HttpRequestMessageFeature(context);                 //HttpRequestMessage req = request.HttpRequestMessage;                  queryOptions = new ODataQueryOptions(queryOptions.Context, Req);             }              return queryOptions.ApplyTo(queryable, );         }     } 

Below line gives error. It needs object of type HttpRequest.

queryOptions = new ODataQueryOptions(queryOptions.Context, Req); 
Add Comment
1 Answer(s)

ODataQueryOptions takes the ASP.NET request type HttpRequest which is available from HttpContext.Request in your controller.

[EnableQuery] [HttpGet] public IQueryable<Product> GetProducts(ODataQueryOptions<Product> queryOptions) {     if (queryOptions.Filter != null)     {         queryOptions = new ODataQueryOptions<Product>(            queryOptions.Context,             HttpContext.Request); // here!!!     }      IQueryable query = queryOptions.ApplyTo(db.Products.AsQueryable());     return query as IQueryable<Product>; }  
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.