locked
How to pass and use optional parameters in C# MVC? RRS feed

  • Question

  • User-1933134441 posted

    I have one call 

    GetSalary(double salary,double allowance)

    Pass Parameter as query string.

    Monday, August 19, 2019 10:01 AM

Answers

  • User-1038772411 posted

    Hi DevTeams,

    Please Refer This Link may help you : https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2019 10:33 AM
  • User-821857111 posted

    If you can make the parameters nullable, then you just need to pass an empty value for the optional one:

    GetSalary(double? salary,double? allowance)

    api/yourendpoint?salary=&allowance=2.3

    Or you can pass in a default value for the "optional" parameter:

    api/yourendpoint?salary=0&allowance=2.3

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2019 1:14 PM

All replies

  • User-1038772411 posted

    Hi DevTeams,

    Please Refer This Link may help you : https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2019 10:33 AM
  • User-821857111 posted

    If you can make the parameters nullable, then you just need to pass an empty value for the optional one:

    GetSalary(double? salary,double? allowance)

    api/yourendpoint?salary=&allowance=2.3

    Or you can pass in a default value for the "optional" parameter:

    api/yourendpoint?salary=0&allowance=2.3

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2019 1:14 PM
  • User1520731567 posted

    Hi DevTeams,

    GetSalary(double salary,double allowance)

    As @Mikesdotnetting said,you not only call it by url,

    If it is a function,you could also set value directly in action,for example:

    GetSalary(1.1,2.2);

    Best Regards.

    Yuki Tao

    Tuesday, August 20, 2019 7:57 AM