【问题标题】:How to serve utility data in Odata?如何在 Odata 中提供实用程序数据?
【发布时间】:2014-08-13 07:35:46
【问题描述】:

我想要一个 UtilityController,它带有一些实用方法来在 Microsoft Asp.NET 的 Odata Api 中提供数据。 目前,如果我想实现一个基于 Product 等实体的控制器,我将拥有一个带有 EDMModelBuit 的 ProductController

  builder.EntitySet<Product>("Product");

如果它们在Product类型中,则与其他类型一起使用,例如

  builder.EntitySet<ProductGroup>("ProductGroup");

现在解决方案是什么如果我想要一个像UtilityController 这样的控制器,其方法为GetAnyListGetOfferTypesPutNewEnumTypeInOfferBase,而我没有任何实用程序类型等,它只是一个与相关的实用程序数据任何实体,也可能不是。例如

 public class UtilityController : ODataController
{
    private DbContext db = new DbContext();

    public List<string> getOfferBase()
    {
        return UtilityService.GetOfferBase();
    }
    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

然后我可以得到这个实用程序数据

 http://localhost:47120/odata/Utility/getOfferBase

【问题讨论】:

    标签: c# web-services asp.net-mvc-4 asp.net-web-api odata


    【解决方案1】:

    未绑定的功能/操作符合您的要求。应用它们,您可以发送此类请求但不需要实体集:

    1. GET http://host/odata/getOfferBase
    2. POST http://host/odata/PutNewEnumTypeInOfferBase
    

    要添加未绑定的函数/动作,您需要这样做:

    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.Function
    
    ActionConfiguration PutNewEnumTypeInOfferBase = modelBuilder.Action("PutNewEnumTypeInOfferBase");
    PutNewEnumTypeInOfferBase .Parameter<string>("Title");
    PutNewEnumTypeInOfferBase .ReturnsFromEntitySet<Movie>("Movies");
    
    builder.Function("GetSalesTaxRate")
                .Returns<double>()
                .Parameter<string>("state");
    

    更多内容请参考: https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataFunctionSample/ https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataActionsSample/

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      相关资源
      最近更新 更多