如何将查询字符串参数传递给 asp.net web api 2

How to pass query string parameter to asp.net web api 2

如何将超过 1 个参数作为查询字符串的一部分传递到我的 asp.net 网站 api 2.

这是我的 asp.net web api 2 方法,我无法弄清楚如何修饰此方法,以便它接受 id 和复杂类型,即 CustomerRequest,我想使用 Url 之类的

http://localhost/api/Customer/?Mobile0012565987&Email=abcxyz.com&IsEmailVerified=true

[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(long id, [FromUri]CustomerRequest request)
        {
            var customer = db.Customers.Find(request.CustomerId);

            if (customer == null)
            {
                return NotFound();
            }

            return Ok(customer);
        }

这是客户请求class

  public class CustomerRequest
    {
        public string Mobile { get; set; }
        public string Email { get; set; }         
        public Nullable<bool> IsEmailVerified { get; set; }    
    }

否则如果有更好的方法,请指导我。

谢谢

根据您的代码,您还需要传递 'id',如下所示:

http://localhost/api/Customer/?id=12345&Mobile=0012565987&Email=abcxyz.com&IsEmailVerified=true

如果你想让'id'可选,你可以让你的方法签名看起来像这样:

public IHttpActionResult GetCustomer([FromUri]CustomerRequest request, long id = 0)

默认情况下会将 id 设置为 0,如果您不在 URL 中传递它。因此,您将能够像最初那样访问您的 URL:

http://localhost/api/Customer/?Mobile=0012565987&Email=abcxyz.com&IsEmailVerified=true