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
ASP.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
ste5an
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
Larry Brister
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
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/
Avatar of ste5an
ste5an
Flag of Germany image

@Andrei, afaik this requires to map each overloaded mtehod to a separate web mtehod in the controller´ as web services don't allow overloading.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo