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

  • 相关阅读:
    Struts 学习之03Controller(控制器 上)
    解决.netFrameWork1.1中事件丢失
    python中的迭代器(iterable)和迭代对象(iterator), type和isinstance
    如何在快速启动栏添加桌面
    使用WebService构建的C/S代码结构示例
    .NET Framework 中的常见命名空间
    多重委托简单示例一
    匿名方法示例之计算器(委托作为参数和返回值)
    委托与事件示例
    批处理文件入门
  • 原文地址:https://www.cnblogs.com/xr0818/p/13072393.html
Copyright © 2011-2022 走看看