StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 4 · Model Binding and Validation

Header And Query Metadata Binding

easy100%1 of 1 solved

Problem statement

Bind a required client id from a request header and an optional locale from the query string straight into the action's parameters.

Requirements

The banner service and the BuildBanner helper are already provided. Your task has one part: bind the two values in GetBanner in StorefrontBannerApi/Controllers/BannerController.cs, and answer with BuildBanner(clientId, locale).

Binding attributes tell ASP.NET Core where a parameter comes from: [FromHeader(Name = "...")] reads a request header and [FromQuery] reads the query string. [BindRequired] makes a value compulsory. Because the controller has [ApiController], a missing or unreadable value is answered with 400 by itself, naming the value.

BuildBanner (provided)

  • It looks the client up and answers 200 with the banner, 404 for an unknown client, or 400 for a locale it has no greeting for

BannerController

  • clientId is an int read from the X-Client-Id header, and it is required: a missing header is answered with 400 whose errors name X-Client-Id, and a value such as abc is answered with 400
  • A client id sent in the query string instead of the header is not used
  • locale is a string read from the query string, and it is optional: when it is left out, it is en-US
Sample Requests & Responses

GET /banner?locale=fr-FR with the header X-Client-Id: 42

Response (200 OK):

{ "clientId": 42, "clientName": "Northwind Traders", "locale": "fr-FR", "message": "Bon retour, Northwind Traders" }

GET /banner with the header X-Client-Id: 42

Response (200 OK), with the default locale:

{ "clientId": 42, "clientName": "Northwind Traders", "locale": "en-US", "message": "Welcome back, Northwind Traders" }

GET /banner with no header

Response (400 Bad Request):

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": { "X-Client-Id": ["A value for the 'X-Client-Id' parameter or property was not provided."] }
}

More in Model Binding and Validation

See the module
Registration Payload BindingThis problem covers binding a JSON request body to a typed record in a minimal API, so a body that cannot be read is answered with 400 before the handler runs.100%1 of 1 solvedeasyAnnotated Product ValidationThis problem covers adding validation attributes to a model and answering an invalid one with validation problem details that name every failing field.100%1 of 1 solvedeasyOrder Submission EndpointThis problem covers binding a route id, a validated JSON body, a required header and an optional query value in one endpoint, and answering bad input with validation problem details naming each failing field.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.