[英]Unit test MiddleWare, how to add a HttpRequest to a HttpContext in .NET Core 3.1
[英]Create a HttpRequest instance in .Net Core 3.1
我必須使用 GET 動詞和 Uri 創建一個HttpRequest
實例,才能將 object 傳遞給OdataQueryOptions
。
下面是 c# 中的示例代碼。 我需要在 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, );
}
}
下面的行給出了錯誤。 它需要HttpRequest
類型的 object 。
queryOptions = new ODataQueryOptions(queryOptions.Context, Req);
ODataQueryOptions
采用 ASP.NET 請求類型HttpRequest
,可從 controller 中的HttpContext.Request
獲得。
[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>;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.