c# - 有没有办法将 HttpGet 方法的参数组合成一个对象?
问题描述
我的控制器中有以下方法。
[HttpGet]
public ActionResult MyGetMethod(string address, string zip, int width) {...}
它被称为通过http://foo.com/home/MyGetMethod?address=234MainSt&zip=90210&width=123
有没有办法将参数地址,zip和宽度组合成一个对象,并以以下方式将对象传递给方法?
[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 可以吗?
解决方案
您在 queryString 中的数据,因此您需要提供者 [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;}
}
推荐阅读
- r - 获取目录中没有子目录名称的文件列表
- android - CrossAxisAlignment.center 永远不会在列中工作
- ios - IOS/AppStore 如何提交现有应用的新版本以供审核
- c++ - 自从迁移到 64 位后,我在随机 glVertexAttribArray 调用中收到来自 nvoglv64.dll 的访问冲突
- authentication - 如何通过 URL 中的个人访问令牌访问 GitHub
- javascript - 如何使用 Node.js MongoDB 驱动程序插入 long/BigInt?
- bash - 如何以root身份打开桌面条目?
- visual-studio - BizTalk 和 Visual Studio 2017
- rust - 为什么这个命令不能正常运行?
- ssh - vscode 远程支持双 ssh 吗?