Asp.net mvc 4 在ASP.NET MVC Web API中禁用JSON支持

Asp.net mvc 4 在ASP.NET MVC Web API中禁用JSON支持,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc 4,Asp.net Web Api,在创建新的基于MVC Web API的服务时,我们希望首先关注XML,并在以后添加JSON功能作为一种增强,使用本机JSON.NET支持的完整版本。为此,我们希望防止服务接受JSON请求或给出JSON响应,以避免建立任何我们希望破坏的功能 有没有办法在ASP.NET MVC API中禁用JSON支持?您所要做的就是删除JSON媒体格式化程序 // Identify JSON formatters in global config. var jsonMediaTypeFormatters = Gl

在创建新的基于MVC Web API的服务时,我们希望首先关注XML,并在以后添加JSON功能作为一种增强,使用本机JSON.NET支持的完整版本。为此,我们希望防止服务接受JSON请求或给出JSON响应,以避免建立任何我们希望破坏的功能


有没有办法在ASP.NET MVC API中禁用JSON支持?

您所要做的就是删除JSON媒体格式化程序

// Identify JSON formatters in global config.
var jsonMediaTypeFormatters = GlobalConfiguration.Configuration.Formatters
    .Where(x => x.SupportedMediaTypes
    .Any(y => y.MediaType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase)))
    .ToList();

// Remove formatters from global config.
foreach (var formatter in jsonMediaTypeFormatters)
{
    GlobalConfiguration.Configuration.Formatters.Remove(formatter);
}

您所要做的就是删除JSON媒体格式化程序

// Identify JSON formatters in global config.
var jsonMediaTypeFormatters = GlobalConfiguration.Configuration.Formatters
    .Where(x => x.SupportedMediaTypes
    .Any(y => y.MediaType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase)))
    .ToList();

// Remove formatters from global config.
foreach (var formatter in jsonMediaTypeFormatters)
{
    GlobalConfiguration.Configuration.Formatters.Remove(formatter);
}

还有一个更短的选项,因为只有一个Json格式化程序,而MediaTypeFormatterCollection将其作为属性公开。从WebAPI2开始提供(可能也在v1中,不确定)


GlobalConfiguration.Configuration.Formatters.Remove(controllerSettings.Formatters.JsonFormatter)

还有一个更短的选项,因为只有一个Json格式化程序,MediaTypeFormatter集合将其作为属性公开。从WebAPI2开始提供(可能也在v1中,不确定)


GlobalConfiguration.Configuration.Formatters.Remove(controllerSettings.Formatters.JsonFormatter)

这就是我期望的解决方案。如果能看到一些关于如何做到这一点的代码,那就太好了。更新。在哪里可以找到GlobalConfiguration对象?这是我所期望的解决方案。如果能看到一些关于如何做到这一点的代码,那就太好了。更新。在哪里可以找到GlobalConfiguration对象?此代码将转到哪里?我的Startup类中没有Formatters属性更新代码这段代码去哪里?我的启动类中没有Formatters属性更新代码