zoukankan      html  css  js  c++  java
  • .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;
    }
    }
    }

  • 相关阅读:
    记一次网易前端实习面试
    你知道吗?Web的26项基本概念和技术
    CSS 颜色代码大全
    研究旧项目, 常用 sql 语句
    Asp.net core 学习笔记 QR code and Barcode
    Html to PDF
    Asp.net core 学习笔记 Node Service
    Asp.net core Identity + identity server + angular + odata + 权限管理
    Angular Material (Components Cdk) 学习笔记 Table
    es6 getter setter
  • 原文地址:https://www.cnblogs.com/xr0818/p/13072393.html
Copyright © 2011-2022 走看看