.Net Core API方法(增删改查)
namespace IOI_API.Controllers
{
//获取路径
[Route("api/[controller]/[action]")]
[ApiController]
public class DefaultController : ControllerBase
{
private IProductDAL _productDAL;
public DefaultController(IProductDAL productDAL)
{
_productDAL = productDAL;
}
/// <summary>
/// 无条件查询获取数据
/// </summary>
/// <returns></returns>
[HttpGet]
public List<ProductModel> GetPros()
{
List<ProductModel> list = null;
try
{
//获取所有数据
list = _productDAL.GetProduct();
}
catch (Exception)
{
throw;
}
//返回获取的数据
return list;
}
/// <summary>
/// get添加数据
/// </summary>
/// <returns></returns>
[HttpGet]
public int AddProduct(string model)
{
ProductModel list = JsonConvert.DeserializeObject<ProductModel>(model);
int code = 0;
try
{
code = _productDAL.AddPros(list);
}
catch (Exception)
{
throw;
}
return code;
}
/// <summary>
///post表单格式添加
/// </summary>
/// <returns></returns>
[HttpPost]
public int Add([FromForm]ProductModel model)
{
int code = 0;
try
{
code = _productDAL.AddPros(model);
}
catch (Exception)
{
throw;
}
return code;
}
//删除
[HttpGet]
public int Delete(int id)
{
int code = 0;
try
{
code = _productDAL.DelPros(id);
}
catch (Exception)
{
throw;
}
return code;
}
//通过id获取修改的数据
[HttpGet]
public ProductModel GetByIdPros(int id)
{
ProductModel list = null;
try
{
//获取所有数据
list = _productDAL.GetById(id);
}
catch (Exception)
{
throw;
}
//返回获取的数据
return list;
}
//post表单格式修改
[HttpPost]
public int EditPro([FromForm]ProductModel model)
{
ProductModel list = JsonConvert.DeserializeObject<ProductModel>(model);
int code = 0;
try
{
code = _productDAL.Edit(list);
}
catch (Exception)
{
throw;
}
return code;
}
}
}
//get修改数据
[HttpGet]
public int EditPro(string model)
{
ProductModel list = JsonConvert.DeserializeObject<ProductModel>(model);
int code = 0;
try
{
code = _productDAL.Edit(list);
}
catch (Exception)
{
throw;
}
return code;
}
}
}