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."] }
}
