Lesson 3
Making Parameters Optional with Nullable Types
Introduction

Welcome to this lesson on advanced model binding in ASP.NET Core Minimal APIs. In our previous lessons, we covered the basics of binding simple types, such as query and route parameters, as well as complex types from the request body. In this lesson, we'll go beyond the basics and explore the concept of making parameters optional, allowing more flexible API interactions without triggering errors when parameters are missing.

All Parameters Are Required

Up until now, all the parameters we've bound—whether they are route values, query parameters, or parts of the request body—were required by default. If any of these parameters were missing in the request, the server would return a 400 Bad Request error. Here's an example to illustrate this:

C#
1app.MapGet("/todos/{id}", (int id) => 2{ 3 var todo = todos.FirstOrDefault(t => t.Id == id); 4 return todo != null ? Results.Ok(todo) : Results.NotFound("Todo not found"); 5});

In this example, the id route parameter is mandatory. If you make a request without specifying the id, the server will respond with a 400 error, indicating a bad request due to missing parameters.

Optional Route Parameter

As a recap from the previous course "Routing in ASP.NET Core," you learned how to make a route parameter optional. Here’s a quick refresher:

C#
1app.MapGet("/todos/{id?}", (int? id) => 2{ 3 if (id.HasValue) 4 { 5 var todo = todos.FirstOrDefault(t => t.Id == id.Value); 6 return todo != null ? Results.Ok(todo) : Results.NotFound("Todo not found"); 7 } 8 return Results.Ok(todos); 9});

In this example, the route parameter id is now optional. If id is not provided, the endpoint responds with all Todo items.

Optional Query Parameter

Next, consider making a query parameter optional. The approach is similar to that of route parameters:

C#
1app.MapGet("/todos", (int? id) => 2{ 3 if (id.HasValue) 4 { 5 var todo = todos.FirstOrDefault(t => t.Id == id.Value); 6 return todo != null ? Results.Ok(todo) : Results.NotFound("Todo not found"); 7 } 8 return Results.Ok(todos); 9});

Here, the query parameter id is optional. The server checks if id has a value and performs the corresponding action. If id is omitted, it returns all Todo items.

Optional Parameter in Request Body

Finally, let's look at how to handle optional parameters within the request body. Similar to route and query parameters, you can use nullable types for body parameters as well:

C#
1app.MapPost("/todos", ([FromBody] TodoItem? todo) => 2{ 3 if (todo != null) 4 { 5 return Results.Ok($"Received Todo: {todo.Title}"); 6 } 7 return Results.BadRequest("No Todo received"); 8});

In this case, the [FromBody] attribute is used to bind the todo object from the request body, and the parameter is made nullable. If the request body is empty, the action gracefully handles it by returning a 400 Bad Request response.

Summary

In this lesson, we explored how to make parameters optional in ASP.NET Core Minimal APIs. By using nullable types, you can create more flexible and adaptable API endpoints, allowing for a broader range of valid requests and smoother client-server interactions. This approach will help enhance the overall API usability and user experience.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.