EntitySetController中的自定义操作生成501未实现

时间:2013-07-10 18:39:00

标签: asp.net-web-api odata breeze

具有多对多关系的域对象:

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?为什么我需要启用它?

1 个答案:

答案 0 :(得分:0)

Breeze还不支持很多关系。作为一种解决方法,您可以使用两个1-many关系。此主题存在用户语音问题; here请投票赞成。