0

在我的项目 CloudMiddleware 中,有用于与 PayPal、Twilio 等 API 集成的服务。我有 SOAP、REST 和 AJAX 请求的端点,我也想使用 ODATA 灵活性,因为请求是通过 HTTP 使用 url 本身。这可能吗?

4

2 回答 2

1

假设您有服务的控制器。每个服务都是具有 Id 和 Description 的类的实例。服务类具有 Id 和 Description 属性。

public class ServiceController : ApiController
{
    // GET api/service
    public IEnumerable<Service> Get()
    {
        return new Service[]
                    {
                        new Service { Id = 1, Description = "This is my service 1." },
                        new Service {Id = 2, Description = "This is my service 2."},
                        new Service {Id = 3, Description = "This is my service 3."}
                    };
    }

    // GET api/service/5
    public Service Get(int id)
    {
        return null;
    }

    // POST api/service
    public void Post([FromBody]string value)
    {
    }
}

public class  Service 
{
    public int Id { get; set; }
    public string Description { get; set; }
}

为了通过 OData 使用控制器的服务,您必须在“Get”方法中使用属性 [Queryable] 并将返回类型更改为 IQueryable ,并且一切就绪!!!!!!,如下:

public class ServiceController : ApiController
{
    // GET api/service
    [Queryable(ResultLimit = 10)]
    public IQueryable<Service> Get()
    {
        return new Service[]
                    {
                        new Service { Id = 1, Description = "This is my service 1." },
                        new Service {Id = 2, Description = "This is my service 2."},
                        new Service {Id = 3, Description = "This is my service 3."}
                    }.AsQueryable();
    }

    // GET api/service/5
    public Service Get(int id)
    {
        return null;
    }

    // POST api/service
    public void Post([FromBody]string value)
    {
    }
}

属性Queryable具有属性ResultLimit,它用于表示可以容纳结果的最大数量的服务实例。它还具有LambdaNestingLimitHandleNullPropagationEnsureStableOrdering属性。

对 /api/service?$top=2 的请求会返回 Json 响应:

{ {“Id”:“1”,“Description”:“这是我的服务 1。”},{“Id”:“2”,“Description”:“这是我的服务 2。”}}

于 2013-07-16T22:53:10.637 回答
0

是的,您可以使用 ASP.NET Web API 或 WCF 数据服务在 ASP.NET MVC 项目中创建 OData 端点。前者为您在端点的实现中提供了更多的控制和灵活性。

于 2013-07-16T13:33:39.267 回答