.net - 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.
解决方案
你可以结合 else if 使用布尔逻辑,如 && || !即使它们是不同的类型
推荐阅读
- javascript - 仅使用reduce计算数组中出现的字符串不读取最后一个值
- c# - 如何删除另一个列表中不存在的值?
- android - 在用户到达平面列表末尾之前,首先调用 onEndReached 函数
- entity-framework - 与 EF 中的许多表建立一对一的关系
- vba - VBA:使用变量引用定义的数据类型元素
- testing - 来自流星测试的乱码测试结果输出:mocha
- javascript - 使用 taggle.js 限制 contenteditable div 的行数
- python - 如何在 Pandas 中将列表连接到数据框
- r - 编写预处理数据代码时发生 R studio 错误
- mysql - mysql值大于值数量的百分比