• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Wednesday, July 1, 2020

Optional Parameters in Web API Attribute Routing

 Admin     July 01, 2020     .Net, Asp.Net, C#, Web API     No comments   

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 article. In our last article, we use the following Student Model

Along with we modify the WebApiConfig class as shown below.

Let’s 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.

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.

In the next article, I am going to discuss Route Prefix in Web API Attribute Routing. Here, in this article, I try to explain the Optional Parameters in Web API Attribute Routing step by step with some examples. I hope this Optional parameter in the Web API Attribute Routing article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Summary:
I Hope this post will be helpful to understand the concept of Optional Parameters in Web API Attribute Routing
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Refresh Token in Web API
    In this article, I am going to discuss how to implement Refresh Token in Web API by validating the clients, as well as I, will also discus...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape