title: Restrict accepted Media Types in ASP.NET Core Controller action
body: <p>I have an ASP.NET Core Service that produces both JSON and XML responses. However, I like to restrict the accepted Media Type for only one action, so Swagger can only list application/json as a valid <strong>response content type</strong>. How can I achieve this in ASP.Net Core?</p>
  
  <p>Please, consider I am using ASP.Net Core (ASP.NET MVC 6), not ASP.NET WebAPI.</p>
  
  <p><a href="https://i.stack.imgur.com/jlAF5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jlAF5.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ok, so I'll add the answer as part of the same question. Thanks to @Helen, I was able to add the required classes to achieve this in ASP.Net Core (ASP.Net MVC 6). The answer is based on <a href="https://stackoverflow.com/a/36348869/3973463">this answer</a> but modified to use ASP.NET Core classes.</p>
  
  <p><strong><em>Step 1.</em></strong> Create a custom action filter attribute so the pipeline reacts to a forbiden content type:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// SwaggerResponseContentTypeAttribute
  /// &lt;/summary&gt;
  [AttributeUsage(AttributeTargets.Method)]
  public sealed class SwaggerResponseContentTypeAttribute : ActionFilterAttribute
  {
      /// &lt;summary&gt;
      /// SwaggerResponseContentTypeAttribute
      /// &lt;/summary&gt;
      /// &lt;param name="responseType"&gt;&lt;/param&gt;
      public SwaggerResponseContentTypeAttribute(string responseType)
      {
          ResponseType = responseType;
      }
      /// &lt;summary&gt;
      /// Response Content Type
      /// &lt;/summary&gt;
      public string ResponseType { get; private set; }
  
      /// &lt;summary&gt;
      /// Remove all other Response Content Types
      /// &lt;/summary&gt;
      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); 
  
      }
  
  }
  </code></pre>
  
  <p><strong><em>Step 2</em></strong>. Create a Swagger Operation Filter so the UI can reflect the restriction</p>
  
  <pre><code>public class ResponseContentTypeOperationFilter : IOperationFilter
  {
  
      public void Apply(Swashbuckle.AspNetCore.Swagger.Operation operation, OperationFilterContext context)
      {
          var requestAttributes = context.ControllerActionDescriptor.GetControllerAndActionAttributes(true).Where(c=&gt;c.GetType().IsAssignableFrom(typeof(SwaggerResponseContentTypeAttribute))).Select(c=&gt; c as SwaggerResponseContentTypeAttribute).FirstOrDefault();
  
          if (requestAttributes != null)
          {
              if (requestAttributes.Exclusive)
                  operation.Produces.Clear();
  
              operation.Produces.Add(requestAttributes.ResponseType);
          }
      }
  }
  </code></pre>
  
  <p><strong><em>Step 3.</em></strong> Configure Swagger UI service in Startup.cs, inside the method ConfigureServices, so it can use the newly created Operation Filter.</p>
  
  <pre><code>    // 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&lt;MvcOptions&gt;(options =&gt;
          {
              options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
  
          });
          // Register the Swagger generator, defining 1 or more Swagger documents
          services.AddSwaggerGen(c =&gt;
          {
              c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
              c.OperationFilter&lt;ResponseContentTypeOperationFilter&gt;();
          });
      }
  </code></pre>
  
  <p><strong><em>Step 4</em></strong>. Annotate the action</p>
  
  <pre><code>    // GET api/values
      [HttpGet]
      [WebService.Utils.SwaggerResponseContentType(responseType: "application/json", Exclusive = true)]
      public IEnumerable&lt;string&gt; Get()
      {
          return new string[] { "value1", "value2" };
      }
  </code></pre>