Asp.net 如何向AspNet.odatav6注册OData序列化程序提供程序

Asp.net 如何向AspNet.odatav6注册OData序列化程序提供程序,asp.net,odata,Asp.net,Odata,Microsoft.AspNet.Odata改变了您注册服务(如序列化程序)的方式。新格式应该是什么样子?您的新配置应该是这样的。您需要至少添加前两个服务(IEdmModel和IEnumerable IODataRoutingConvention),然后您可以添加自己的服务。例如,自定义odata序列化程序提供程序 using Boomerang.OData; using Boomerang.Models; using Microsoft.OData; using Microsoft.OData

Microsoft.AspNet.Odata改变了您注册服务(如序列化程序)的方式。新格式应该是什么样子?

您的新配置应该是这样的。您需要至少添加前两个服务(IEdmModel和IEnumerable IODataRoutingConvention),然后您可以添加自己的服务。例如,自定义odata序列化程序提供程序

using Boomerang.OData;
using Boomerang.Models;
using Microsoft.OData;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Formatter;
using System.Web.OData.Formatter.Serialization;
using System.Web.OData.Routing.Conventions;

namespace Boomerang
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.MapODataServiceRoute(
                "ODataRoute",
                "api",
                containerBuilder => containerBuilder
                .AddService<IEdmModel>(ServiceLifetime.Singleton, s => BuildEdmModelForOData())
                .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
                    ODataRoutingConventions.CreateDefaultWithAttributeRouting("ODataRoute", config))
                .AddService<ODataSerializerProvider>(ServiceLifetime.Singleton, s => new CustomODataSerializerProvider(s))
                );

            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
        }

        public static IEdmModel BuildEdmModelForOData()
        {
            ODataConventionModelBuilder
                oDataConventionModelBuilder = new ODataConventionModelBuilder();

            // Build model here
            oDataConventionModelBuilder.EntitySet<Post>("Posts");
            oDataConventionModelBuilder.EntitySet<Feeling>("Feelings");

            return oDataConventionModelBuilder.GetEdmModel();
        }
    }
}
使用Boomerang.OData;
使用回飞棒模型;
使用Microsoft.OData;
使用Microsoft.OData.Edm;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Http;
使用System.Web.OData.Builder;
使用System.Web.OData.Extensions;
使用System.Web.OData.Formatter;
使用System.Web.OData.Formatter.Serialization;
使用System.Web.OData.Routing.Conventions;
命名空间回飞棒
{
公共静态类WebApiConfig
{
公共静态无效寄存器(HttpConfiguration配置)
{
//Web API路由
config.maphttpAttribute路由();
config.MapODataServiceRoute(
“ODataRoute”,
“api”,
containerBuilder=>containerBuilder
.AddService(ServiceLifetime.Singleton,s=>buildedModelForOData())
.AddService(ServiceLifetime.Singleton,sp=>
ODataRoutingConventions.CreateDefaultWithAttributeRouting(“ODataRoute”,config))
.AddService(ServiceLifetime.Singleton,s=>new CustomODataSerializerProvider)
);
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
}
公共静态IEdmModel buildedModelForOData()
{
ODataConventionModelBuilder
oDataConventionModelBuilder=新oDataConventionModelBuilder();
//在这里建立模型
oDataConventionModelBuilder.EntitySet(“Posts”);
oDataConventionModelBuilder.EntitySet(“感觉”);
返回oDataConventionModelBuilder.GetEdmModel();
}
}
}
我之所以添加这个答案,是因为大多数在线指南都给出了6之前版本的说明。此信息可在此处找到: