gpt4 book ai didi

c# - ASP.NET WebAPI HttpGet 操作 - 查询字符串后模型为空

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:02 24 4
gpt4 key购买 nike

下面的查询字符串没有被 MVC 操作反序列化。操作成功,但我在操作中得到了 searchModel 的空值。

https://test.api.domain.com:9090/mont/contact/searchemployee?lastname=Smith

编辑:简化模型

型号

public class EmployeeSearchModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

Action :

    [HttpGet]
public List<Employee> SearchEmployee(EmployeeSearchModel searchModel)
{
List<Employee> employees = new List<Employee>();
try
{
if (searchModel != null)
{
//some logic
}
else
{
//dirty feedback for testing - this is what the action returns
employees.Add(new Employee { FirstName = "searchModel was null" });
}
}
catch (Exception e) { WriteFileLog(_logPath, e.ToString()); }

return employees;
}

最佳答案

是 ASP.NET MVC 还是 Web API?从您从 Controller 操作而不是 ActionResult 返回员工列表这一事实,我可以看出它是 Web API。如果是这样,您可以将 [FromUri] 属性应用于模型:

public List<Employee> SearchEmployee([FromUri]EmployeeSearchModel searchModel)

关于c# - ASP.NET WebAPI HttpGet 操作 - 查询字符串后模型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26386200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com