【问题标题】:Custom action in EntitySetController generate 501 Not ImplementedEntitySetController 中的自定义操作生成 501 Not Implemented
【发布时间】:2013-07-11 05:26:10
【问题描述】:

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

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 库用于 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&amp;$expand=Tags&amp;$select=Id,CompanyName,Phone,Tags/Id,Tags/Name&amp;tagName=#5

问题

我做错了什么?为什么会出现 501 错误?

在我的班级 WebApiConfig 中。我需要这个代码EnableQuerySupport?为什么我需要启用它?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2021-03-13
      • 2018-11-03
      相关资源
      最近更新 更多