欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

创建第一个Web API项目

第一步:

 

第二步:

 

 注:App_Start\WebApiConfig

第三步:创建Model、Controll,略

 

 

--分别创建对应实体
 public class User
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string phone { get; set; }
        public int role { get; set; }
    }

public class Queues
    {
        public string name { get; set; }
    }

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class UserController : ApiController
    {
        [HttpGet]
        public string Getuser(string username, string password)
        {
            // API 名字 最好是以请求方式开头 
            //假如你是Get 请求  最好是GetXXX
            return username + password;
        }

        [HttpGet]
        public int GetMaxAge(int age1, int age2)
        {
            if (age1 > age2) return age1;
            return age2;
        }


    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class QueuesController : ApiController
    {
        [HttpGet]
        public string GetQueuesByName(string queueName)
        {
            // API 名字 最好是以请求方式开头 
            //假如你是Get 请求  最好是GetXXX
            return queueName;
        }

        [HttpGet]
        public int GetQueuesCount(string type)
        {
            return 10;
        }
    }
}

注:获取访问参数——HttpContext.Current.Request.QueryString["IdCardNo"]

访问方式可参考:http://localhost:32766/api/User/id?username=1&password=2

 

posted on 2021-03-12 10:58  sunwugang  阅读(75)  评论(0编辑  收藏  举报