C #: utilizzo di Odata con input di array nell'API

Oct 08 2020

Come faccio a far funzionare Odata con una variabile List? La seguente API non funziona e dà errori.

HttpGet

Errore: {"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "traceId": " 00-b0b48a41f445ac4bbcebba902f8cca95-a714db4f4fd0c146-00 "}

Tutte le mie altre API funzionano,

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

Controller:

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

Servizio:

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

Risultato pagina:

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

Risposte

go.. Oct 08 2020 at 12:11

Questo problema indica che c'è un problema con la tua richiesta, ma anche se sei venuto a rispondere, ci sono errori che riceverai. Non puoi dare un valore che la sessione sia aperta in risposta

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