I've been learning about Web API recently, and making plans to increase the scalability of my MVC apps, using it. When I finally got into creating a Web API controller, though, I discovered a [Produces("application/json")]
annotation applied to the controller class. I haven't been able to figure out what that annotation does. I want the controller to only accept json input, so is this tag helpful to me?
Produces Attribute This attribute helps the user to inform the framework to generate and send the output result always in given content type as follows.
Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.
DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.
DataAnnotations namespace includes the following validator attributes: Range – Enables you to validate whether the value of a property falls between a specified range of values. RegularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.
The ProducesAnnotation only matters the response formatting. So this is not helpful for your need to restrict your input.
You can direct the content negotiation process to a specific type for the output of controller actions or a specific action by using ProducesAnnotation from the ASP.NET Core MVC framework. From the documentation (https://docs.asp.net/en/latest/mvc/models/formatting.html):
If you would like to restrict the response formats for a specific action you can, you can apply the [Produces] filter.
If you want to restrict inputs to json on a global level you could configure MVC on startup to have only a single InputFormatter of type JsonInputFormatter in your Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
...
// Add framework services.
services.AddMvc(config =>
{
// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Clear();
config.InputFormatters.Add(new JsonInputFormatter());
});
...
}
On Controller or Action level the counterpart of [Produces] is the [Consumes] Annotation. With
[Consumes("application/json")]
public class MyController : Controller
{
public IActionResult MyAction([FromBody] CallModel model)
{
....
}
}
calls to this controller will only succeed if the client provides Content-Type header of application/json. Otherwise a 415 (Unsupported Media Type) will be returned.
Hope this helps.
Yes, the Produces attribute restricts the response to only the types defined. You can add more than one by using the attribute this way.
[Produces("application/json", "application/xml")]
ASP.NET Core uses a default value of JSON. If you want xml as well you will also have to specify the xml formatter in the ConfigureServices method.
services.AddMvc()
.AddMvcOptions(o => o.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With