简体   繁体   English

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

[英]ASP.NET WebAPI HttpGet Action - model is null after querystring

The querystring below is not being deserialized by the MVC action. MVC操作不会反序列化以下查询字符串。 The action gets hit ok, but I'm getting null value for searchModel in the action. 动作正常,但动作中的searchModel得到空值。

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

EDIT : Simplified Model 编辑 :简化模型

Model 模型

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;
    }

Is that ASP.NET MVC or Web API? 那是ASP.NET MVC还是Web API? From the fact that you return a list of employees from the controller action instead of ActionResult I can see that it's Web API. 从您从控制器操作而不是ActionResult返回员工列表的事实来看,我可以看到它是Web API。 If so, you could apply [FromUri] attribute to the model: 如果是这样,您可以将[FromUri]属性应用于模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM