QueryString始终为空ASP.net Core 3.1

问题描述 投票:0回答:2

我使用的查询字符串始终为空。我不知道为什么,并尝试了几个小时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)
        {

Empty Query

model-binding asp.net-core-3.1
2个回答
0
投票

由于您正在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]


0
投票

确定,我找到了答案。我本应在CREATE Using GET to get querystring

的GET部分中使用它时,尝试在CREATE的POST中使用它

感谢大家的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.