Home > Enterprise >  How to pass option query string parameter which is of string type in Webapi?
How to pass option query string parameter which is of string type in Webapi?

Time:10-01

I need to pass a string parameter as querystring parameter which should be optional.

public IHttpActionResult Test([FromUri] string Name, string Place)

Here I want Place as an optional parameter. I tried to use as string?Place=null. But It wont works for me. Let me know the solution for this.

CodePudding user response:

You were close:

public IHttpActionResult Test([FromUri] string Name, string Place = null)

CodePudding user response:

if you don't want to sen Place just don't put it in your url

...controller/test?Name=name

if you need both parameters

...controller/test?Name=name&Place=place

and you have to use FromUri for both parameters

public IHttpActionResult Test([FromUri] string Name, [FromUri]string Place)

or maybe you can try dont'use at all

public IHttpActionResult Test( string Name, string Place)
  • Related