簡體   English   中英

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

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

我試圖通過將 id 從 Angular 9+ 傳遞到 ASP.NET Core Web API 來刪除該列,但我無法在控制器中點擊。 我在這里犯了什么錯誤? 我正在使用表數據來運行 SQL 查詢。

控制器

[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;
        }
    }

結算服務.ts文件:

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

打字稿文件:

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');
            }
      );
   }
}

錯誤:

刪除 https://localhost:44372/api/Settlement/DeleteById/355 404

您的route路徑已被此屬性[HttpDelete("{id}")]覆蓋

所以只需刪除 Route 屬性並像這樣添加您的屬性

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

接下來需要把方法參數中的[FromBody]屬性去掉,這樣寫

public IActionResult DeleteSettlementById(int id)

希望它有幫助 - 快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM