how to set optional parameter in .net core 6 get method?
Loading
how to set optional parameter in .net core 6 get method?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Mar 8, 2023, 9:08 AM EST
[HttpGet("GetById/{id?}")]example
[HttpGet("GetById/{id:int?}")] public Product GetById(int id = 1) { var products = Get(); return products.Where(p => p.Id == id).FirstOrDefault()!; }Vishal JoshiRank #377 · 6.7K reputationPosted Mar 9, 2023, 1:15 AM EST
Hello Mukund
Pass optional parameters with route constraints.
[HttpGet("GetById/{id:int?}")] public Student GetById(int id = 1) { return Student.FirstOrDefault(s => s.Id == id); }The default value will be 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.
[HttpGet("GetById/{id:int=1}")] public Student GetById(int id) { return Student.FirstOrDefault(s => s.Id == id); }Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Mar 9, 2023, 1:34 AM EST
You can make any method with optional parameters by just assigning a default value to that parameter.
just keep in mind optional parameter should always come at the last of all parameters.
public int method(int x=1) { ///to do } public int method(int x=1, int y) // this is wrong and won't work { ///to do } public int method(int x, int y=1) //this is correct { ///to do } public int method(string Name, int age=18) //this is correct { ///to do } public int method(int age,string Name="sachin") //this is correct { ///to do }for web app and API you can also use route concept as discussed by others.
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 9, 2023, 1:35 AM EST
Hello Mukund,
You can make a URI parameter optional by adding a question mark to the route parameter.
Example :
[HttpGet("GetStudent/{studentId?}")] public IActionResult GetStudent(string studentId) {...You can refer below link:
https://stackoverflow.com/a/36669280
Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 12:30 AM EDT
Hello Mukund,
You can set optional parameters in methods using default parameter values. Check this sample snippet:
public void MyMethod(int requiredParam, string optionalParam = "default value") { // method body }In the example above, optionalParam is the optional parameter with a default value of "default value". If the caller doesn't provide a value for optionalParam, the default value will be used.
You can also use named arguments to skip optional parameters and provide values for later parameters in the method signature.
MyMethod(123); // calls MyMethod with requiredParam = 123, optionalParam = "default value" MyMethod(456, "custom value"); // calls MyMethod with requiredParam = 456, optionalParam = "custom value" MyMethod(requiredParam: 789); // calls MyMethod with requiredParam = 789, optionalParam = "default value"Optional parameters must be the last parameters in the method signature. You can't have a required parameter after an optional parameter.