C #: การใช้ Odata กับ Array Input ใน API

Oct 08 2020

ฉันจะให้ Odata ทำงานกับตัวแปร List ได้อย่างไร API ต่อไปนี้ไม่ทำงานและเกิดข้อผิดพลาด

HttpGet

ข้อผิดพลาด: {"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "traceId": " 00-b0b48a41f445ac4bbcebba902f8cca95-a714db4f4fd0c146-00 "}

API อื่น ๆ ทั้งหมดของฉันใช้งานได้

"http: // localhost: 4547 / api / properties / GetIdentifierPaged"

ตัวควบคุม:

[HttpGet[("[Action]")] 
public ActionResult GetIdentifierPaged(List<string> propertyListRequest, ODataQueryOptions<PropertyDto> queryOptions)
{
    propertyListRequest.Add("1110200100"); // fake data
    var model =  _propertyService.GetByPropertyIdentifierPaged(propertyListRequest).ToODataPageResult(queryOptions);
    return Ok(model);
}

บริการ:

public IQueryable<PropertyDto> GetByPropertyIdentifierPaged(List<string> identifiers)
{
    var identifiersEnumerable = identifiers.AsEnumerable();
    var properties = _dataContext.Property
        .Include(x => x.PropertyStatus)
         Where(x => identifiersEnumerable.Contains(x.PropertyIdentifier))
    return properties;
}

ผลลัพธ์ของเพจ:

public static PageResult<T> ToODataPageResult<T>(this IQueryable<T> query, ODataQueryOptions<T> queryOptions)
{
    var settings = new ODataQuerySettings();
    settings.EnsureStableOrdering = false;
    var countQuery = (IQueryable<T>)queryOptions.ApplyTo(query, settings, AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.Select | AllowedQueryOptions.OrderBy);
    long count = countQuery.Count();
    var results = (IQueryable<T>)queryOptions.ApplyTo(query, settings, AllowedQueryOptions.None);

    return new PageResult<T>(results, null, count);
}

คำตอบ

go.. Oct 08 2020 at 12:11

ปัญหานี้บ่งชี้ว่ามีปัญหากับคำขอของคุณ แต่แม้ว่าคุณจะตอบกลับ แต่ก็ยังมีข้อผิดพลาดที่คุณจะได้รับ คุณไม่สามารถให้ค่าที่เซสชันเปิดอยู่ในการตอบกลับ

return new PageResult<T>(**results.ToList()**, null, count);