C #: Verwenden von Odata mit Array-Eingabe in der API
Oct 08 2020
Wie bringe ich Odata dazu, mit einer List-Variablen zu arbeiten? Die folgende API funktioniert nicht und gibt einen Fehler aus.
HttpGet
Fehler: {"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Nicht unterstützter Medientyp", "status": 415, "traceId": " 00-b0b48a41f445ac4bbcebba902f8cca95-a714db4f4fd0c146-00 "}
Alle meine anderen APIs funktionieren,
"http: // localhost: 4547 / api / properties / GetIdentifierPaged"
Regler:
[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);
}
Bedienung:
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;
}
Seitenergebnis:
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);
}
Antworten
go.. Oct 08 2020 at 12:11
Dieses Problem weist darauf hin, dass ein Problem mit Ihrer Anfrage vorliegt. Selbst wenn Sie zur Antwort gekommen sind, werden Fehler angezeigt. Sie können keinen Wert angeben, für den die Sitzung als Antwort geöffnet ist
return new PageResult<T>(**results.ToList()**, null, count);