2

I have a function as below:

        [HttpGet]
        [Route("Departments/{departmentid}/employeeByDeptId")]
        [ResponseType(responseType: typeof(IList<Employee>))]
        public HttpResponseMessage GetDetailsByDeptId(int departmentId, DateTime? sinceDate)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            var detailInfo = _employeeManager.GetDetailInfo(departmentId, sinceDate ?? _sinceDate);

            return CreateHttpResponseMessage(detailInfo);

        }

I have made sinceDate as Nullable type in declaration so that if null value is passed it takes the date which is there in '_sinceDate' variable declared locally. Now how can I pass null value to sinceDate parameter while making an API call.

When I pass below url: http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate=2020-03-03

I get the desired result. Now I want to pass sinceDate as null. I tried http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate=null http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate isnull

All gave bad request error. Please tell how can I assign null value to sinceDate in API call?

1
  • 1
    simply deleting sinceDate from queryString is not working. I have to declare this sinceDate=null in function definition too. Commented Apr 6, 2020 at 12:58

2 Answers 2

2

Simply add parameterName = null in your route parameter.

public HttpResponseMessage GetDetailsByDeptId(int departmentId, DateTime? sinceDate = null){
}

Then in your request, you could just exclude that parameter and access with;

http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId

Another option is add an overload. Have 2 function names receive different parameters.

public HttpResponseMessage GetDetailsByDeptId(int departmentId, DateTime sinceDate){
}

public HttpResponseMessage GetDetailsByDeptId(int departmentId){
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this. But why I have to declare parameter = null when I have made sinceDate as Nullable type already
@kanika it's a precaution because there might be something yet to be setup in your controller and your controller is not accepting the parameters being sent. Adding a default value will immediately assign a null value if the key isn't found in your request parameters.
At least for an int?, I'm getting 405 Method not allowed. 😕
1

For anyone reading this, as far as I can tell, the accepted answer doesn't work (in itself) anymore. See https://github.com/dotnet/aspnetcore/issues/6878#issuecomment-647830903 for more details and the 2 different alternative ways around it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.