Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Odata的Web API返回BSON,同时期望响应为JSON_C#_Json_Asp.net Web Api_Odata - Fatal编程技术网

C# 使用Odata的Web API返回BSON,同时期望响应为JSON

C# 使用Odata的Web API返回BSON,同时期望响应为JSON,c#,json,asp.net-web-api,odata,C#,Json,Asp.net Web Api,Odata,我严格遵循了这个教程, 除了作为JSON的返回格式,但不确定我如何返回BSON,我对BSON一无所知 我使用的是OData、WebAPI、EF(存储库模式)。关于如何取回JSON而不是BSON的任何建议 这是我的代码,到目前为止我做了什么 WebApiConfig: public static void Register(HttpConfiguration config) { // Web API configuration and services

我严格遵循了这个教程,

除了作为JSON的返回格式,但不确定我如何返回BSON,我对BSON一无所知

我使用的是OData、WebAPI、EF(存储库模式)。关于如何取回JSON而不是BSON的任何建议

这是我的代码,到目前为止我做了什么

WebApiConfig:

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            // config.SuppressDefaultHostAuthentication();
            // config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes           
            // Remove the XML formatter
            config.Formatters.Remove(config.Formatters.XmlFormatter);                
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/v1/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
ODataConfig.cs-在routeConfig.cs中使用它

 public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes(); //This has to be called before the following OData mapping, so also before WebApi mapping
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Item>("Item");
            config.MapODataServiceRoute("ODataRoute", "api", builder.GetEdmModel());
        }
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
控制器。

        [EnableQuery]
        [ODataRoute]
        public IQueryable<Item> GetItem()
        {
            var result = _cRepository.GetAll();            
            return result.AsQueryable();
        }
[启用查询]
[ODataRoute]
公共IQueryable GetItem()
{
var result=_cRepository.GetAll();
返回结果。AsQueryable();
}

我的控制器引用了System.Web.Http.OData,卸载了与OData、WebAPI相关的所有nuget软件包并重新安装。引用Controller到System.Web.OData,现在一切看起来都很好,返回JSON。非常感谢您的回复。

是的,请尝试显示重现您问题的是。
如果需要,我可以共享我的代码。
S.O用于回答特定的编码问题。所以这确实是你应该做的(但不是一堆文字,a请)。我看不到你从哪里得到的代码BSON@EZI感谢您的响应,来自控制器的响应将返回result.AsQueryable();我应该会收到JSON,但由于某种原因,响应内容类型是Application/b一旦发布请求头,您可以吗?可能是请求请求返回BSON,而Web API正在执行。请在此处查看有关内容协商的信息: