【发布时间】:2020-04-06 11:34:52
【问题描述】:
我的功能如下:
[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);
}
我在声明中将 sinceDate 设置为 Nullable 类型,因此如果传递了 null 值,它将采用本地声明的 '_sinceDate' 变量中的日期。 现在如何在进行 API 调用时将 null 值传递给 sinceDate 参数。
当我通过以下网址时: http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate=2020-03-03
我得到了想要的结果。 现在我想将 sinceDate 作为 null 传递。我试过了 http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate=null http://localhost:26754/v1/EmployeeMgnt/Departments/4/employeeByDeptId?sinceDate isnull
所有都给出了错误的请求错误。请告诉我如何在 API 调用中将 null 值分配给 sinceDate?
【问题讨论】:
-
仅仅从 queryString 中删除 sinceDate 是行不通的。我也必须在函数定义中声明这个 sinceDate=null。
标签: c# asp.net asp.net-mvc nullable webapi