简体   繁体   English

按 id 删除行在 Asp.net 核心 Web API + Angular 9 中不起作用

[英]Deleting row by id not working in Asp.net core web API + Angular 9

I am trying to delete the column by passing id from Angular 9+ to ASP.NET Core Web API, but I am not able to hit in the controller.我试图通过将 id 从 Angular 9+ 传递到 ASP.NET Core Web API 来删除该列,但我无法在控制器中点击。 What mistake I have done here?我在这里犯了什么错误? I am using table data to run SQL queries.我正在使用表数据来运行 SQL 查询。

Controller控制器

[Route("api/[controller]")]
[ApiController]
public class SettlementController : ControllerBase
{
    public IConfiguration Configuration { get; }
    private readonly DatabaseContext _context;

    public SettlementController(IConfiguration configuration, DatabaseContext context)
    {
        Configuration = configuration;
        _context = context;
    }

    // Delete settlement by id
    [Route("DeleteById")]
    [HttpDelete("{id}")]
    public IActionResult DeleteSettlementById([FromBody] SettlementModel model) //sealed class of having Id only
    {
        var tid = model.Id;

        try
        {
            string sConn = Configuration["ConnectionStrings:ServerConnection"];

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

                // This is the stored procedure. I pass in the "Id"
                string query = "ST_PRO_DELETESETTLEMENT"; 

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

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

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            objList.Add(new
                            {
                                //TID = reader[0].ToString(),
                            });
                        }
                    }

                    con.Close();
                }

                return Ok(objList);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

settlementService.ts file:结算服务.ts文件:

 deleteSettlement(id) {
     console.log(id);
     return this.http.delete(this.BaseURL + 'Settlement/DeleteById/' + id);
 }

Typescript file:打字稿文件:

deleteSettle(tid) {
    console.log(tid);  // getting tid in console

    if (confirm('Are you sure to delete this record ? TID: '+ tid)) {
        this.settlementService.deleteSettlement(tid).subscribe(
            (data: any) => {
                this.toastr.success("Successfully deleted");
            },
            err => {
               if (err.status == 400)
                  this.toastr.error('Server error or bad request', 'Error');
               else
                  this.toastr.error('Failed to check data', 'Server Error');
            }
      );
   }
}

Error:错误:

DELETE https://localhost:44372/api/Settlement/DeleteById/355 404删除 https://localhost:44372/api/Settlement/DeleteById/355 404

Your route path has been overridden by this attribute [HttpDelete("{id}")]您的route路径已被此属性[HttpDelete("{id}")]覆盖

So just remove the Route attribute and add your attribute like this所以只需删除 Route 属性并像这样添加您的属性

[HttpDelete("DeleteById/{id}")]

Next you need to remove the [FromBody] attribute in the method parameters and write like this接下来需要把方法参数中的[FromBody]属性去掉,这样写

public IActionResult DeleteSettlementById(int id)

Hope it helps - Happy coding :)希望它有帮助 - 快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM