I want to filter my data with OData V4 based on an enum field. I want to do this without including the entire namespace of the enum in the query string.
I want to run my code by launching this url https://localhost:7282/weatherforecast?$filter=testEnum%20eq%20%271%27 instead of https://localhost:7282/weatherforecast?$filter=testEnum%20eq%20TestOData.TestEnum%271%27
I have this code:
using Microsoft.AspNetCore.OData;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers().AddOData(opt =>
opt.Select().Filter().OrderBy());
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
public enum TestEnum
{
Test1,
Test2,
Test3
}
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
[EnableQuery]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)],
TestEnum = TestEnum.Test2
})
.ToArray();
}
}
To filter the data based on the enum field "testEnum" I need to run the following url: https://localhost:7282/weatherforecast?$filter=testEnum%20eq%20TestOData.TestEnum%271%27
How do I get this code to work by launching the following url (without the enum namespace): https://localhost:7282/weatherforecast?$filter=testEnum%20eq%20%271%27
When I run this generate this exception:
{"Message":"The query specified in the URI is not valid. The string '1' is not a valid enumeration type constant.","ExceptionMessage":"The string '1' is not a valid enumeration type constant.","ExceptionType":"Microsoft.OData.ODataException","StackTrace":" at Microsoft.OData.UriParser.MetadataBindingUtils.ConvertToTypeIfNeeded(SingleValueNode source, IEdmTypeReference targetTypeReference)\r\n at Microsoft.OData.UriParser.BinaryOperatorBinder.PromoteOperandTypes(BinaryOperatorKind binaryOperatorKind, SingleValueNode& left, SingleValueNode& right, TypeFacetsPromotionRules facetsPromotionRules)\r\n at Microsoft.OData.UriParser.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n at Microsoft.OData.UriParser.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.OData.UriParser.FilterBinder.BindFilter(QueryToken filter)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNetCore.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNetCore.OData.Query.Validator.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuting(ActionExecutingContext actionExecutingContext)"}