Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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/0/asp.net-core/3.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# ASP.NET Core 3.0/Swashback:全局限制响应内容类型_C#_Asp.net Core_Swashbuckle_Asp.net Core 3.0 - Fatal编程技术网

C# ASP.NET Core 3.0/Swashback:全局限制响应内容类型

C# ASP.NET Core 3.0/Swashback:全局限制响应内容类型,c#,asp.net-core,swashbuckle,asp.net-core-3.0,C#,Asp.net Core,Swashbuckle,Asp.net Core 3.0,这基本上与.NETCore3.0的问题相同 默认情况下,在.NET Core 3.0中,您可以使用服务配置web api。AddControllers(),然后使用服务配置Swashblock。addSwagger()+应用程序。UseSwagger() 这很好,但是swagger.json包含每个操作的多个响应内容类型(text/plain+application/json+text/json) 我可以通过在我的操作中添加[products]和[Consumes]来限制这些响应内容类型,但我希

这基本上与.NETCore3.0的问题相同

默认情况下,在.NET Core 3.0中,您可以使用
服务配置web api。AddControllers()
,然后使用
服务配置Swashblock。addSwagger()
+
应用程序。UseSwagger()

这很好,但是swagger.json包含每个操作的多个响应内容类型(text/plain+application/json+text/json)

我可以通过在我的操作中添加
[products]
[Consumes]
来限制这些响应内容类型,但我希望在每个操作中都避免这样做(即,我希望在全局范围内这样做)

请注意,我更希望使用System.Text.Json,但如果您有一个只适用于Newtonsoft.Json的解决方案,那么它总比没有好;)

您可以创建自定义

然后

services.AddSwaggerGen(cfg=>cfg.OperationFilter());
swashback.AspNetCore.swagggen5.0使用来描述API操作

using System.Collections.Generic;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

public class AssignContentTypeFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (operation.Responses.ContainsKey("200"))
        {
            operation.Responses.Clear();
        }

        var data = new OpenApiResponse
        {
            Description = "Ok",
            Content = new Dictionary<string, OpenApiMediaType>
            {
                ["application/json"] = new OpenApiMediaType(),
                ["application/xml"] = new OpenApiMediaType(),
            }
        };

        operation.Responses.Add("200", data);
    }
}
使用System.Collections.Generic;
使用Microsoft.OpenApi.Models;
使用swashback.AspNetCore.SwaggerGen;
公共类AssignContentTypeFilter:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
if(operation.Responses.ContainsKey(“200”))
{
operation.Responses.Clear();
}
var数据=新的OpenApiResponse
{
Description=“Ok”,
内容=新词典
{
[“application/json”]=新的OpenApiMediaType(),
[“application/xml”]=新的OpenApiMediaType(),
}
};
操作。响应。添加(“200”,数据);
}
}
在Startup.cs中

        services.AddSwaggerGen(q =>
        {
            q.SwaggerDoc("v1", new OpenApiInfo
            {
                Title = "mytitle",
                Version = "v1",
            });
            q.OperationFilter<AssignContentTypeFilter>();  
        });
services.AddSwaggerGen(q=>
{
q、 SwaggerDoc(“v1”),新OpenApiInfo
{
Title=“mytitle”,
Version=“v1”,
});
q、 操作过滤器();
});

Swashback.AspNetCore.SwaggerGen 6+的版本。仅删除
text/plain
内容类型。在我的例子中,对字符串结果有效

内部类RemoveTextContentOperationFilter:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
foreach(operation.Responses中的var(u,response)
{
var=response.Content
其中(kvp=>kvp.Key==“文本/普通”);
foreach(错误内容中的var错误内容)
{
response.Content.Remove(错误内容);
}
}
}
}
为字符串操作生成的摆动:

“/api/news/{newsactionid}/file link”:{
“获取”:{
“标签”:[
“新闻API”
],
“操作ID”:“NewsApi\u GetUploadFileLink”,
“参数”:[
{
“名称”:“newsArticleId”,
“在”:“路径”,
“必需”:正确,
“模式”:{
“类型”:“整数”,
“格式”:“int32”
}
}
],
“答复”:{
"500": {
“说明”:“服务器错误”,
“内容”:{
“应用程序/json”:{
“模式”:{
“$ref”:“#/components/schemas/apiror”
}
},
“text/json”:{
“模式”:{
“$ref”:“#/components/schemas/apiror”
}
}
}
},
"401": {
“说明”:“未经授权”,
“内容”:{
“应用程序/json”:{
“模式”:{
“$ref”:“#/components/schemas/apiror”
}
},
“text/json”:{
“模式”:{
“$ref”:“#/components/schemas/apiror”
}
}
}
},
"200": {
“说明”:“成功”,
“内容”:{
“应用程序/json”:{
“模式”:{
“类型”:“字符串”
}
},
“text/json”:{
“模式”:{
“类型”:“字符串”
}
}
}
}
}
}
},

这个想法是可行的,但在新的5.0.0-rc4中,您需要使用operation.RequestBody.Content和operation.Responses[].ContentGreat,您可以将此注释添加到answer@ChristopheBlin我需要在哪里使用它们?@FrancoScarpa您在回答中提到的向services.AddSwagerGen注册过滤器很抱歉否决投票,但这个回答没有回答我的问题(与公认的答案相反),只是解释过滤器(我在问题中已经提到)
using System.Collections.Generic;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

public class AssignContentTypeFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (operation.Responses.ContainsKey("200"))
        {
            operation.Responses.Clear();
        }

        var data = new OpenApiResponse
        {
            Description = "Ok",
            Content = new Dictionary<string, OpenApiMediaType>
            {
                ["application/json"] = new OpenApiMediaType(),
                ["application/xml"] = new OpenApiMediaType(),
            }
        };

        operation.Responses.Add("200", data);
    }
}
        services.AddSwaggerGen(q =>
        {
            q.SwaggerDoc("v1", new OpenApiInfo
            {
                Title = "mytitle",
                Version = "v1",
            });
            q.OperationFilter<AssignContentTypeFilter>();  
        });