我创建了一个 Web Api OData 控制器并尝试直接查看它,但它不起作用。
WebApiConfig 配置
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute("odata", "odata", GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static IEdmModel GetEdmModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
builder.Namespace = "PackageManager.Models";
return builder.GetEdmModel();
}
在 App 开始调用注册:
WebApiConfig.Register(GlobalConfiguration.Configuration);
API 控制器:
public class CustomersController : EntitySetController<Customer, string>
{
NorthwindDbContext _Context = new NorthwindDbContext();
[Queryable]
public override IQueryable<Customer> Get()
{
return _Context.Customers;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_Context.Dispose();
}
}
尝试访问:localhost:2375/odata/Customers
然后总是出错:
无法找到该资源。