Link to home
Create AccountLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

C# WebMethod Nullable

How do I make a C# web method optional?

 [WebMethod]
    public string tabApiADSetLead(string user, string token, string location)
... etc

string loctaion to be optional
Avatar of ste5an
ste5an
Flag of Germany image

You don't.

Maybe you should consider rephrasing your question and providing more context.
Avatar of Larry Brister

ASKER

ste5an
You are usually helpful... and maybe you don;t see the tone in your question?

OK... In a C# WebMwthod I need a paramater to be an optional one

I "think" this should work?

add... using System.Runtime.InteropServices;

change to string location to this? 
[Optional, DefaultParameterValue(null)] string Location
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
In C# you can declare several versions of a method:
// 1
public string tabApiADSetLead(string user, string token, string location){
   ...
}
// 2
public string tabApiADSetLead(string user, string token) {
   tabApiADSetLead(user, token, ""); // it calls previous function with the location = ""
}
If you use tabApiADSetLead(user, token, location) it executes (1).
If you use tabApiADSetLead(user, token) it executes (2). In (2) you can replace "" with any default value.

It has the name "Method overloading". Read details here: https://www.geeksforgeeks.org/c-sharp-method-overloading/
@Andrei, afaik this requires to map each overloaded mtehod to a separate web mtehod in the controller´ as web services don't allow overloading.