Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Produces Data Annotation

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?

like image 966
Nick Fulton Avatar asked Jul 16 '16 19:07

Nick Fulton


People also ask

What is the purpose of using the produces data attribute with your API?

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.

How do you use data annotations?

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.

What is data annotation in MVC?

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.

How do you validate data annotations?

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.


2 Answers

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.

like image 170
Ralf Bönning Avatar answered Sep 19 '22 16:09

Ralf Bönning


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()));
like image 21
John C Avatar answered Sep 21 '22 16:09

John C