.net webapi 基础入门使用

创建新项目,选择创建 webapi项目


1. 创建完成后点击 Controllers 文件夹,单机右键,选择添加控制器

 2.点击确定
3.选择api控制器

 4.点击确定

 5. 更具自己需求,修改 [Route("api/[controller]")], 我这点改为 [Route("api/[controller]/[action]")]

6.自定义一个 api 方法 GetProducts
Products.cs文件我放到最下面

public class ProductsController : ControllerBase
{
    [HttpGet]
    // 第一种写法,swagger不能显示返回的格式
    //public IActionResult GetProducts()
    //{
    //    var products =  Products.ListAll();
    //   return new JsonResult(products);
    //}

    //推荐使用下面这种方式
    public List<Products> GetProducts()
    {
        var products = Products.ListAll();
        return products;
    }
}

 

7.此时运行swagger 找不到json文件
8. 添加  [HttpGet] 就解决了

完整代码:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SwaggerTest.Model;

namespace SwaggerTest.Controllers
{
    [Route("api/[controller]/[action]")]
    //[ApiController]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetProducts()
        {
            var products =  Products.ListAll();
           return new JsonResult(products);
        }
    }
}

运行结果:

 

Products.cs文件内容

using SwaggerTest.Utility;
using System.Data;

namespace SwaggerTest.Model
{
    public class Products
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public string ProductImage { get; set; }
        public decimal Price { get; set; }
        public decimal OrgPrice { get; set; }
        public string Decoratio { get; set; }
        public string Size { get; set; }
        public int ClickTimes { get; set; }
        public int SaleTimes { get; set; }
        public bool IsDel { get; set; }

        public static List<Products> ListAll()
        {
            string sql = "select * from Products";
            DataTable dt = SqlHelper.ExecuteTable(sql);
            List<Products> products = new List<Products>();
            foreach (DataRow dr in dt.Rows)
            {
                products.Add(dr.DtToModel<Products>());
            }
            return products;
        }
    }
}
SqlHelper.cs文件
using System.Data;
using System.Data.SqlClient;
namespace SwaggerTest.Utility
{
    public class SqlHelper
    {
        public static string connStr {  get; set; }
        public static DataTable ExecuteTable(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}
ToModel.cs 文件
using System.Data;

namespace SwaggerTest.Utility
{
    public static class ToModel
    {
        public static T DtToModel<T>(this DataRow row)
        {
            Type type = typeof(T);
            T t = (T)Activator.CreateInstance(type);
            foreach (var prop in type.GetProperties())
            {
                prop.SetValue(t, row[prop.Name]);
            }
            return t;
        }
    }
}

 



 

posted @ 2024-05-24 14:55  龙卷风吹毁停车场  阅读(183)  评论(0)    收藏  举报