首页 > 解决方案 > Can we mix different request type together in one method?

问题描述

In my angular application, I call the API by passing an object. I just have one button click event. The business logic contains multiple database operation by this simple click.

On the server side, I have to decide to insert or update or delete records from entity framework.

My question is for the convenience, can I mix different type together in one method?

Code sample:

[HttpPatch]
[HttpPost]
[HttpDelete]
public ActionResult InsertOrUpdateOrDeleteByCondition([Required][FromBody]MyDto body)
{
    if(body.value == "condition1")
    {
       dbContext.MyEntity.Add(body);
    }
    else if(body.value == "condition2")
    {
       dbContext.MyEntity.Update(body);
    }
    else if(body.value == "condition3")
    {
        // delete first
        // then insert new value
    }
    else
    {
       dbContext.MyEntity.Remove(body);
    }
    dbContext.SaveChanges();
    return Json("Good job");
}

I set a break point at the line SaveChanges(). It did reach there, however I found the table is not changed.

标签: .netentity-framework.net-core

解决方案


你可以结合 else if 使用布尔逻辑,如 && || !即使它们是不同的类型


推荐阅读