Loading

ASP.NET Web API

What is Optional Parameters in Web API Attribute Routing?. The Complete ASP.NET Web API Developer Course 2023 [Videos].

In this article, I am going to discuss Optional Parameters in Web API Attribute Routing with some examples. Please read our previous article before proceeding to this article as we are going to work with the same example that we started in the Web API Attribute Routing article where we discussed the following things.

  1. Why we need attribute routing?
  2. What is Attribute Routing?
  3. How to Implement Attribute Routing?
  4. What are the advantages of using Attribute Routing?
Optional Parameters in Web API Attribute Routing and Default Values:

You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

We are going to work with the same example that we created in our last Video. In our last Video, we use the following Student Model

Optional URI Parameters in Attribute Routing and Default Values

Along with we modify the WebApiConfig class as shown below.

Optional URI Parameters in Attribute Routing and Default Values

Lets modify the Student Controller as shown below.
namespace AttributeRoutingInWEBAPI.Controllers
{
public class StudentsController : ApiController
{
static List<Student> students = new List<Student>()
{
new Student() { Id = 1, Name = "Pranaya" },
new Student() { Id = 2, Name = "Priyanka" },
new Student() { Id = 3, Name = "Anurag" },
new Student() { Id = 4, Name = "Sambit" }
};
// Optional URI Parameter with default value
// URL: /api/students
// URL: /api/students/1
[Route("api/students/{stdid:int?}")]
public Student GetBooksByID(int stdid = 1)
{
return students.FirstOrDefault(s => s.Id == stdid);
}
}
}

In the above example, /api/students and /api/students/1 return the same resource. Alternatively, you can also specify a default value inside the route template as shown in the below image.

Optional URI Parameters in Attribute Routing and Default Values

This is almost the same as the previous example, but there is a slight difference in the behavior when the default value is applied.

In the first example (“{stdid?}”), here the default value 1 is directly assigned to the action method parameter, so the method parameter will have this value exactly.

In the second example (“{stdid=1}”), the default value “1” assigned to the method parameter through the model-binding process. The default model-binder in Web API will convert the value “1” to the numeric value 1. 

In most of the cases, unless you have custom model binders in your pipeline, the two forms will be equivalent.

See All

Comments (328 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Web API / Blog

What is ASP.NET Web API Application?

In this ASP.NET Web API Tutorials series, I covered all the features of ASP.NET Web API. You will learn from basic to advance level features of ASP.NET Web API. The term API stands for “Application Programming Interface” and ASP.NET Web API is a framework provided by Microsoft which makes it easy to build Web APIs, i.e. it is used to develop HTTP-based web services on the top of .NET Framework.
3-Feb-2022 /34 /328

ASP.NET Web API / Blog

How to creat ASP.NET Web API Application using Visual Studio?

In this article, I am going to discuss the step-by-step procedure for Creating ASP.NET Web API Application. Please read our previous article before proceeding to this article where we gave an overview of the ASP.NET Web API framework. As part of this article, we ate going to discuss the following pointers.
3-Feb-2022 /34 /328

ASP.NET Web API / Blog

How to add Swagger in Web API Application?

In this article, I am going to discuss how to add Swagger in Web API Application to document and test restful Web API services. Please read our previous article where we discussed How to Create an ASP.NET Web API Application step by step before proceeding to this article as we are going to work with the same example. As part of this article, we are going to discuss the following pointers.
3-Feb-2022 /34 /328