public class Customer
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Customer> Customers { get; set; }
}
public class CustomersController : EntitySetController<Customer, int>
{
// .. omited
[HttpGet]
public IQueryable<Customer> GetByTag([FromODataUri] string tagName)
{
tagName = tagName.Replace("#", "");
return _Context.Customers.Where(p => p.Tags.Any(t => t.Name.Contains(tagName)));
}
}
这是因为我使用Breeze liberary进行odata请求,并且她做了不支持odata的方法any
。
public static class BreezeWebApiConfig
{
public static void RegisterBreezePreStart()
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi",
routeTemplate: "api/{controller}/{action}"
);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute("odata", "odata", GetEdmModel());
config.EnableQuerySupport();
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");
var customersByTagAction = builder.Entity<Customer>().Collection.Action("GetByTag");
customersByTagAction.Parameter<string>("tagName");
customersByTagAction.ReturnsCollectionFromEntitySet<Customer>("Customers");
builder.EntitySet<Tag>("Tags");
builder.Namespace = "WebAPIODataWithBreezeConsumer.Models";
return builder.GetEdmModel();
}
}
/odata/Customers/GetByTag?$orderby=CompanyName&$expand=Tags&$select=Id,CompanyName,Phone,Tags/Id,Tags/Name&tagName=#5
我做错了什么?为什么错误501?
在我的班级WebApiConfig中。我需要这段代码EnableQuerySupport
?为什么我需要启用它?