我使用的查询字符串始终为空。我不知道为什么,并尝试了几个小时HttpContext.Request返回URL的所有其他部分(查询字符串除外)。
使用此网址https://localhost:44394/Trackers/Create?Place=Vision_College
和此模型
[BindProperties(SupportsGet = true)]
public partial class Tracker
{
[FromQuery(Name = "Place")] //populates it from the query
public string Place { get; set; }
...}
和此控制器
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name, Phone, Place")] Tracker tracker)
{
由于您正在httpPost中使用查询参数,因此应在参数内使用[FromQuery]
。按照此
您的DTO班级将是,
public class Tracker
{
[FromQuery(Name = "Place")]
public string Place{ get; set; }
}
在您的控制器类中
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([FromQuery]Tracker tracker)
{
}
[注意:如果查询参数与模型属性名称匹配,则无需特别注释属性。
更好的是您自己可以得到,因为这是一个职位要求。否则,将此作为获取请求。如果要转换为按主体获取,只需在端点参数中使用[FromBody]
而不是[FromQquery]