【问题标题】:How to post JSON data to controller in ASP.NET Core Web API?如何在 ASP.NET Core Web API 中将 JSON 数据发布到控制器?
【发布时间】:2021-01-19 16:58:49
【问题描述】:

我想将 Angular 中的数据(PostDateTime、Name、Division)作为参数发布到控制器,但我无法传递。

打字稿:

onSubmit(){
this.salesService.postGetData(this.form.value).subscribe(
  (res: any) => {
    console.log(this.form.value);  //I got data here, working fine upto here but cannot post to controller
    this.recData = res;
  },
  err => {
    if (err.status == 400)
      this.toastr.error('Error !!', 'Authentication failed.');
    else
      this.toastr.error('Network error', 'Authentication failed.');
      //this.loadSpinner.hide();
   }
 );

Service.ts:

 postGetData(data) {
     return this.http.post(this.BaseURL + 'Report/FilterCollectionReport', data);
 }

控制器:

[Route("FilterCollectionReport")]
[HttpPost]
public IActionResult CollectionReportFilter(string PostDateTime, string Name, string Division) // tried by adding [FormBody] also but does not work
{
    string iConn = Configuration["ConnectionStrings:IConnection"];

    using (SqlConnection con = new SqlConnection(iConn))
    {
        List<Object> objList = new List<Object>();

        string query = "KK_SP_VIEWSALESENTRY"; // It is SQL procedure takes some parameter/s

        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name; 
            cmd.Parameters.Add("@PostDateTime", SqlDbType.VarChar).Value = PostDateTime;
            cmd.Parameters.Add("@Division", SqlDbType.VarChar).Value = Division;

            con.Open();
            cmd.Connection = con;

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    objList.Add(new
                                {
                                   Id = reader[0].ToString(),
                                   Name = reader[2].ToString(),
                                   Division = reader[3].ToString(), //...etc
                                });
                }
            }

            con.Close();
        }

        return Ok(objList);
    }
}

当我从 Params 发布数据但不能从 body --> raw --> JSON 发布数据时,它在 Postman 中完美运行

没有类,我正在使用现有数据库..

我有什么遗漏需要添加到我的代码中吗?

【问题讨论】:

    标签: asp.net angular asp.net-mvc asp.net-core asp.net-web-api


    【解决方案1】:

    似乎您在请求正文中发送对象数据,因此您需要为其创建模型并将其用作FromBodyAttribute的控制器方法参数。

    public sealed class DataModel {
        public string PostDateTime {get; set;}
    
        public string Name {get; set;}
    
        public string Division {get; set;}
    }
    

    并像这样使用它:

    [Route("FilterCollectionReport")]
    [HttpPost]
    public IActionResult CollectionReportFilter([FormBody] DataModel model) 
    {
        var name = model.Name;
        var division = model.Division;
        var postDateTime = model.PostDateTime
        // Your logic
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2018-02-14
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2016-05-28
      • 2023-02-04
      相关资源
      最近更新 更多