限制ASP.NET Core Controller操作中接受的媒体类型

Alf*_* A. 6 asp.net media-type swagger-ui asp.net-core

我有一个同时生成JSON和XML响应的ASP.NET Core Service。但是,我只想限制一个动作接受的媒体类型,因此Swagger只能将application / json列为有效的响应内容类型。如何在ASP.Net Core中实现此目标?

请考虑我正在使用ASP.Net Core(ASP.NET MVC 6),而不是ASP.NET WebAPI。

在此处输入图片说明

更新

好的,所以我将答案添加为同一问题的一部分。感谢@Helen,我能够在ASP.Net Core(ASP.Net MVC 6)中添加所需的类来实现此目的。答案基于此答案,但已修改为使用ASP.NET Core类。

步骤1.创建一个自定义操作过滤器属性,以便管道对禁止的内容类型做出反应:

/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class SwaggerResponseContentTypeAttribute : ActionFilterAttribute
{
    /// <summary>
    /// SwaggerResponseContentTypeAttribute
    /// </summary>
    /// <param name="responseType"></param>
    public SwaggerResponseContentTypeAttribute(string responseType)
    {
        ResponseType = responseType;
    }
    /// <summary>
    /// Response Content Type
    /// </summary>
    public string ResponseType { get; private set; }

    /// <summary>
    /// Remove all other Response Content Types
    /// </summary>
    public bool Exclusive { get; set; }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var accept = context.HttpContext.Request.Headers["accept"];
        var accepted = accept.ToString().ToLower().Contains(ResponseType.ToLower());
        if (!accepted)
            context.Result = new StatusCodeResult((int)HttpStatusCode.NotAcceptable); 

    }

}
Run Code Online (Sandbox Code Playgroud)

第二步。创建一个Swagger操作过滤器,以便UI可以反映限制

public class ResponseContentTypeOperationFilter : IOperationFilter
{

    public void Apply(Swashbuckle.AspNetCore.Swagger.Operation operation, OperationFilterContext context)
    {
        var requestAttributes = context.ControllerActionDescriptor.GetControllerAndActionAttributes(true).Where(c=>c.GetType().IsAssignableFrom(typeof(SwaggerResponseContentTypeAttribute))).Select(c=> c as SwaggerResponseContentTypeAttribute).FirstOrDefault();

        if (requestAttributes != null)
        {
            if (requestAttributes.Exclusive)
                operation.Produces.Clear();

            operation.Produces.Add(requestAttributes.ResponseType);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第3步。在Startup.cs中的ConfigureServices方法内配置Swagger UI服务,以便它可以使用新创建的Operation Filter。

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.Configure<MvcOptions>(options =>
        {
            options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());

        });
        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            c.OperationFilter<ResponseContentTypeOperationFilter>();
        });
    }
Run Code Online (Sandbox Code Playgroud)

第四步。注释动作

    // GET api/values
    [HttpGet]
    [WebService.Utils.SwaggerResponseContentType(responseType: "application/json", Exclusive = true)]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
Run Code Online (Sandbox Code Playgroud)

Mor*_*lon 14

我知道这是一个老问题,但在这里我试图这样做,所以,为了让其他人更容易,我将用“通用”方式添加到 @joeystdio 的答案中,以添加相同的生产/消费属性所有端点。(对于我的用例,为所有端点进行设置比逐一设置更容易。

.AddControllers(options  => 
{
    options.Filters.Add(new ProducesAttribute("application/json"));
    options.Filters.Add(new ConsumesAttribute("application/json"));

    // if you need a specific response type.
    options.Filters.Add(new ProducesResponseTypeAttribute(typeof(ApplicationNotification), StatusCodes.Status500InternalServerError));
})
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么这被否决了,这全面修复了它!专业提示:您可以使用 MediaTypeNames.Application.Json 而不是对字符串进行硬编码。 (6认同)

小智 8

您可以使用注释消费和生产。这些也由Swashbuckle领取。像这样:

[HttpGet]
[Consumes("application/xml")]
[Produces("application/xml")]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}
Run Code Online (Sandbox Code Playgroud)