对 ASP.Net Core 很陌生,尤其是 API。 在过去,我习惯于使用默认 Controller 创建简单的 api,例如:
[Produces("application/json")]
[Route("api/TestCust")]
public class TestCustController : Controller
{
// GET: api/TestCust
[HttpGet]
public IEnumerable<Customer> Get()
{
IEnumerable<Customer> customer = null;
....
return customer;
}
// GET: api/TestCust/5
[HttpGet("{id}", Name = "Get")]
public IEnumerable<Customer> Get(int id)
{
IEnumerable<Customer> customer = null;
return customer;
}
现在我遇到了一个新的挑战,因为我正在制作一个 API,但客户端已经由第三方制作。这意味着,我不得不按照他们的方式去做。
幸运的是,它有很好的文档记录,并且他们提供了将发送到我的 API 的请求示例。其中一个请求如下:/Customers?$filter=ID+eq+guid'1D225D75-A587-4AE4-BA9A-2224B2484EA5'
为了获得所有客户:/Customers ?$orderby=代码&$skip=100
现在,我完全是 OData 的新手,我昨天才知道这些,并且我一直在关注一些关于它的教程。虽然,他们中的大多数都在使用 Entity Framework ,而我正在结合存储过程使用 Dapper。
随后的教程:https://damienbod.com/2018/10/12/odata-with-asp-net-core/ , https://dotnetthoughts.net/perform-crud-operations-using-odata-in-aspnet-core/
所以我一直在尝试使用[EnableQuery]
发出请求
但这还没有成功。
我会在底部链接错误。
那么,我到底做了什么?好吧,我更改 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOData();
services.AddODataQueryFilter()
services.AddMvc();
services.AddOptions();
}
在我的客户 Controller 中:
public class CustomersController : ODataController{
...
[EnableQuery]
public IEnumerable<Customer> Get()
{
IEnumerable<Customer> allCustomers = null;
IEnumerable<Addresses> allAddresses = null;
IEnumerable<Contacts> allContacts = null;
//Execute the procedures to fetch our objects.
using (var connection = new SqlConnection(config.Value.ConnectionString.ToString()))
{
allCustomers = connection.Query<Customer>("someproc");
allAddresses = connection.Query<Addresses>("someproc");
allContacts = connection.Query<Contacts>("someproc");
}
//Loop through our customer object
foreach(var item in allCustomers)
{
//Initialize a collection of address + contact
ICollection<Contacts> CustomerContact = null;
ICollection<Addresses> CustomerAddress = null;
//Bind the Contact and Address to the correct Customer using the CustomerID
//Tijdelijk uitgezet omdat customer nu even geen GUID is..
//CustomerContact = allContacts.Where(x => x.Customer == item.Id).ToList();
//CustomerAddress = allAddresses.Where(x => x.Customer == item.Id).ToList();
item.Contacts = CustomerContact;
item.Addresses = CustomerAddress;
}
return allCustomers;
}
这是它在浏览器/ postman 中作为“错误 400 错误请求”返回的消息:
The query specified in the URI is not valid. Cannot find the services container for the non-OData route. This can occur when using OData components on the non-OData route and is usually a configuration issue. Call EnableDependencyInjection() to enable OData components on non-OData routes. This may also occur when a request was mistakenly handled by the ASP.NET Core routing layer instead of the OData routing layer, for instance the URL does not include the OData route prefix configured via a call to MapODataServiceRoute()
最佳答案
这里有几件事.. 首先,要使用 OData,您需要返回 IQueryable,而不是 IEnumerable。此外,我不确定 Dapper 是否可以与 OData 一起使用,因为 OData 是一种主要设计用于与 EF 和“实体”一起使用的 MS 技术。我认为您可能能够让它工作,但您需要拼凑某种解决方案,该解决方案采用 ODataQueryOptions 输入并将它们映射到 EF 端的 IQueryable。 (基本上,不容易)
关于c# - 没有 Entity Framework 但存储过程的 ASP.Net Core 2.0 API OData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54788603/