reach2saurabh
reach2saurabh

Reputation: 163

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

Upvotes: 0

Views: 1676

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190943

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

Upvotes: 2

Related Questions