fetch

ASP NET Web API query string parameters



Sharing buttons:

this is part 11 of asp.net Web API

tutorial in this video we'll discuss

using query string parameters in asp.net

Web API let's understand the query

string parameters with an example at the

moment the get method that we have in

our employees controller returns the

list of all employees we want to modify

this method in such a way that it

returns employees by gender so here is

what we want to do notice in the URI we

are specifying gender using a query

string parameter if the value for this

parameter is all then we want the method

to return all employees if it's male

then only male employees if it's female

then only female employees we want to

make this query string gender parameter

optional if we don't specify this

parameter at all then we still want the

get method to return the list of hall

employees and the value for this

parameter should be either all male or

female if it is anything else then we

want the get method to return a status

code of 400 bad request but the message

saying value for gender must be male

female are all ABC is invalid so let's

see how to achieve this let's flip to

Visual Studio now the first thing that

I'm going to do here is include a

parameter of type string the name of the

parameter is going to be gender I am

also going to specify a default value

this default value makes this parameter

optional so in case if we don't specify

a value for this parameter then it's

going to use this default value I am

also going to change the return type of

this method from ienumerable of employee

to HTTP response message and then inside

our using block let's switch on this

gender parameter value so let's use the

switch statement and let's first convert

gender parameter value to lowercase so

we can compare it with lowercase strings

and then let's use case all so when the

value for gender parameter is all then

we want to return the list of all

employees so let's use the request

object dot create response in this case

the HTTP status code is going to be okay

and then we want to return the list of

all employees so entities dot employees

dot to list let's get rid of this return

statement from here and now let's make a

copy of this case statement now if the

value for gender is male then we want to

return only male employees so entities

dot employee is dot where employee such

that employee dot gender let's convert

this to lowercase so we can compare it

with a lowercase string so employee door

gender door to lawyer equals male and

then convert that we our list and return

let's make a copy of this one in case if

the gender is female then we want to

return only female employees now if the

value is not all male or female then it

falls to the default case in which case

we want to return status code 400 with a

message so here return let's use the

request object again dot this time we

are going to use create error response

and the HTTP status code is going to be

bad request and we also want to specify

a message and the message is going to be

this value for gender must be all male

or female and we also want to include

whatever value have specified for

example if we have specified a DC which

is invalid that will be in this gender

parameter so let's include that as well

here is invalid

all right so let's give our solution a

build build succeeded

now let's issue a request from the

browser notice within the URI right here

we don't have the gender query string

parameter at all so in this case it

should return both male and female

employees now let's include the gender

query string parameter gender equals

female so in this case it should return

only female employees this gender query

string parameter will be mapped to this

gender parameter right here so let's

issue the request notice we get only

female employees let's change this to

male notice we are getting only male

employees and if we change it to all we

should get all both male and female

employees here is the code that we just

discussed thank you for listening and

have a great day