Introduction
In general, one should invoke the method with the required parameters, either from the another method or from the UI part as sending the parameters by service (AJAX). Here, I have been working on a project within the banking domain that has an Employee level interaction. There's a Scenario that I had been faced is that, I need to call a function based upon Start Date , End Date ( These are mandatory) and Account number or Card number ( Either any one of it is required ). So I had managed in Controller by using If Conditions based on Null or Not Null values of the optional parameters. I thought It would be fine as the function is being called based on the If Condition until unless I got the report from the SIT team.
As I am working on the banking project, each and every minute, details will get checked thoroughly out of box and I will be notified if there is any necessity of modification. In this particular context, I got the report that details that they had been tested using third party tools ( Eg: PostMan ) and they had noticed that when they try to call a function using three parameters (which are required) from the front end, it is passing four parameters out, while the fourth parameter is stating its value as null or undefined. Though its value is null they raised it as modification due to security that whatever the values are passing should be the mandatory and no extra parameter is allowed to pass either its value is null also. So to come up with this modification, I found that there's an option in MVC to make the parameter as Optional.
Step 1
Declare the Namespace
- using System.Runtime.InteropServices;
Step 2
In this context, I am going to show you how to make parameter as optional from passing one method to another method.
Method 1
Declare the method which is to be called by another method
- public string Parametertest(sting i, string j, [Optional] string k,[Optional] string l)
- {
- string m = i + j;
- if (k != null)
- m += k;
- if (l != null)
- m += l;
- return m;
- }
Here in the above method, I declared parameters to be optional by using optional attributes. This method requires two parameters as mandatory and two parameters as optional.
Method 2
Now we can test the above method using different cases.
Case 1

Dipa MehtaPosted Mar 7, 2020, 7:35 AM
Nice article, Santosh...