有没有一种方法可以将HttpGet方法的参数组合到一个对象中?

时间:2019-05-09 16:39:30

标签: c# .net .net-core asp.net-core-mvc

我的控制器中有以下方法。

[HttpGet]
public ActionResult MyGetMethod(string address, string zip, int width) {...}

通过http://foo.com/home/MyGetMethod?address=234MainSt&zip=90210&width=123

调用

有没有一种方法可以将地址,邮政编码和宽度参数组合成一个对象,并按照以下方式将对象传递给方法?

[HttpGet]
public ActionResult MyGetMethod(Foo myParam) {...}

public class Foo {
   public string Address {get; set;}
   public string Zip {get; set;}
   public int Width {get; set;}
}

对于旧的ASP.NET MVC,存在一个模糊的相关问题,似乎表明这对于该技术是不可能的。

.NET Core 2.x是否可以?

1 个答案:

答案 0 :(得分:1)

您在queryString中的数据,因此您需要provider [FromQuery]

  

[FromQuery]

[HttpGet]
public ActionResult MyGetMethod([FromQuery]Foo myParam) {...}

public class Foo {
   public string Address {get; set;}
   public string Zip {get; set;}
   public int Width {get; set;}
}
相关问题