所以我有一个使用Axios的服务来调用我的C#API。由于我想选择特定的数据,所以我使用带有参数的get方法。
这是我的服务:
let response = await Axios.get('/api/get-report', {
params: filter
});
以下是我的typescript中的过滤器对象:
export interface FilterModel {
employeeId?: string;
Month?: Date;
from?: Date;
to?: Date;
}
这是服务器上的型号:
public class AttendanceReportFilterModel
{
public string EmployeeId { set; get; }
public DateTime? Month { set; get; }
public DateTime? From { set; get; }
public DateTime? To { set; get; }
}
这是我的C#API:
[HttpGet("get-report")]
public async Task<IActionResult> GetReport(FilterModel filter)
{
var Detail = await Service.GetReport(filter);
if (Detail == null)
{
return StatusCode(500, "Not Found");
}
return Ok(Detail);
}
每当我呼叫我的服务时,它总是返回Bad Request
。
有人知道为什么以及如何解决这个问题吗?
尝试添加
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
所以,既然你正在绑定对象,你就需要说把它们带到哪里https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#用属性自定义模型绑定行为。
或者你可以只使用参数
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)