ASP.net Rest API始终以JSON格式返回响应

时间:2018-03-16 11:00:42

标签: asp.net json rest bson odata-v4

我有一个ASP.NET REST API,我使用" EF Desinger From Database"创建了ADO.Net实体数据模型。来自我的Maria DB。使用了实体框架6。 我使用Odata v4控制器。

我的问题是:我总是把Json作为回应

我试过了:

config.Formatters.JsonFormatter.SupportedMediaTypes.Clear();
config.Formatters.Remove(config.Formatters.JsonFormatter);
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");

但没有区别

WebApiConfig.cs中的Register方法如下所示

  public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();       
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
        builder.EntitySet<mOne>("mOne");
        builder.EntitySet<mTwo>("mTwo");
        builder.EntitySet<mThree>("mThree");

        var defaultConventions = ODataRoutingConventions.CreateDefault();
        var conventions = new[] {
            new CustomConvetion()
        }.Concat(defaultConventions.Except(
            defaultConventions.OfType<MetadataRoutingConvention>()));
        config.MapODataServiceRoute(
            "DefaultApi",
            "api",
            builder.GetEdmModel(),
            pathHandler: new CustomPathHandler(),
            routingConventions: conventions);


        //config.Routes.MapODataServiceRoute("DefaultApi", "api", builder.GetEdmModel());
        //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
        //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/bson"));
        //var bson = new BsonMediaTypeFormatter();
        //bson.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.contoso"));
        //config.Formatters.Add(bson);
        //config.Formatters.XmlFormatter.UseXmlSerializer = true;
        //config.Formatters.Add(new BsonMediaTypeFormatter());
        //var xml = config.Formatters(config.Formatters.XmlFormatter);

        //config.Formatters.JsonFormatter.SupportedMediaTypes.Clear();
        //config.Formatters.Remove(config.Formatters.JsonFormatter);
        //var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        //xml.UseXmlSerializer = true;
        //config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
        //config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
    }

我想使用BSON和JSON作为输出格式,但是如果有人可以帮助我使用除Json之外的任何东西作为输出格式,那么首先我很好。

0 个答案:

没有答案