我的asp.net核心控制器有一个简单的模型:
[HttpPost]
public async Task<DefaultResponse> AddCourse([FromBody]CourseDto dto)
{
var response = await _courseService.AddCourse(dto);
return response;
}
Run Code Online (Sandbox Code Playgroud)
我的模型是:
public class CourseDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Genre { get; set; }
public string Duration { get; set; }
public string Level { get; set; }
public string AgeRange { get; set; }
public string Notes { get; set; }
public bool Active { get; set; }
public string OrganisationCode …Run Code Online (Sandbox Code Playgroud) model-binding custom-model-binder asp.net-core-mvc asp.net-core
我正在使用 .net core 3+ Web api。
下面是我的操作的样子,它使用 HTTP GET,我想传递几个字段,其中一个字段是整数列表。
[HttpGet]
[Route("cities")]
public ActionResult<IEnumerable<City>> GetCities([FromQuery] CityQuery query)
{...}
Run Code Online (Sandbox Code Playgroud)
这是CityQuery课程 -
public class CityQuery
{
[FromQuery(Name = "stateids")]
[Required(ErrorMessage = "stateid is missing")]
public string StateIdsStr { get; set; }
public IEnumerable<int> StateList
{
get
{
if (!string.IsNullOrEmpty(StateIdsStr))
{
var output = StateIdsStr.Split(',').Select(id =>
{
int.TryParse(id, out var stateId);
return stateId;
}).ToList();
return output;
}
return new List<int>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种通用方法可以用来接受整数列表作为输入而不接受字符串然后解析它?
或者有更好的方法来做到这一点吗?我尝试谷歌搜索但找不到太多。提前致谢。
asp.net-core ×2