客户是第三方,所以我无权访问该来源。但是,我有一个很好解释的PDF,其中包含客户端使用的一些请求示例。 例如,
/Customers?$filter=ID+eq+guid'1D225D75-A587-4AE4-BA9A-2224B2484EA5'
/Customers?$filter=Code+eq+12345
这是我第一次使用OData和Entity Framework,目前我在过滤方面遇到问题。
当我执行不过滤的简单GET请求时,它会正确返回数据。 但是,当我做类似
的操作时http://localhost:52973/odata/customers?$filter=Code+eq+12069
它返回错误:
{"error":{"code":"","message":"The query specified in the URI is not valid. A binary operator with incompatible types was detected. Found operand types 'Edm.String' and 'Edm.Int32' for operator kind 'Equal'.","details":[],"innererror":{"message":"A binary operator with incompatible types was detected. Found operand types 'Edm.String' and 'Edm.Int32' for operator kind 'Equal'.","type":"Microsoft.OData.ODataException","stacktrace":" 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.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNet.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass1_0.<OnActionExecuted>b__3(ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)"}}}
所以让我们开始吧。在我们的Startup.cs中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
app.UseMvc(b =>
{
b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers").EntityType
.Filter(nameof(Customer.Id),nameof(Customer.Code));
return builder.GetEdmModel();
}
然后是我们的控制器:
public class CustomersController : ODataController
{
private readonly IsahContext db; //IsahContext is our DbContext class ;)
public CustomersController()
{
this.db = new IsahContext();
}
[HttpGet]
[EnableQuery]
public IActionResult Get()
{
var AllCusts = GetAllCustomers();
return Ok(AllCusts);
}
[HttpGet]
[EnableQuery]
public IActionResult Get(Guid Id, string Code)
{
var AllCusts = GetAllCustomers();
if (!string.IsNullOrEmpty(Code))
{
return Ok(AllCusts.FirstOrDefault(c => c.Code == Code));
}
return Ok(AllCusts.FirstOrDefault(c => c.Id == Id));
}
public IList<Customer> GetAllCustomers()
{
IList<Customer> customers = null;
customers = db.Customers.FromSql("SomeProcName").ToList();
return customers;
}
答案 0 :(得分:3)
删除加号(+
)并将值包装在单引号中,因为它应该是字符串相等性检查。最后一个uri应该是:
http://localhost:52973/odata/customers?$filter=Code eq '12069'
如果您要使用编码的uri,请使用%20
表示空格,使用%27
表示单引号:
http://localhost:52973/odata/customers?$filter=Code%20eq%20%2712069%27