C#搭建一个简单的web api项目

前言

刚刚了解web api ,然后试着搭建了一个web api 的项目。如有错误请指出。

正文

新建一个asp.net 的项目,然后选择 空 web api 项目。
在这里插入图片描述

建成以后,可以看出,和mvc非常的相似。
在这里插入图片描述
第一步:修改路由
在 App_start 中,web api 是没有action 的路由,这一点和mvc有些不同,为了方便,我们修改路由和mvc一样~(添加 action)
在这里插入图片描述

第二步:添加一个控制器
在这里插入图片描述
第三步:编码

在控制器中:添加一个post兼get请求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using static Web_api实践.ApiTools;

namespace Web_api实践.Controllers
{
    public class UserInfoController : ApiController
    {
        //检查用户名是否已注册
        private ApiTools tool = new ApiTools();
        [HttpPost]
        [HttpGet]
        public HttpResponseMessage CheckUserName(string _userName)
        {
            int num = UserInfoGetCount(_userName);//查询是否存在该用户
            if (num > 0)
            {
                return tool.MsgFormat(ResponseCode.操作失败, "不可注册/用户已注册", "1 " + _userName);
            }
            else
            {
                return tool.MsgFormat(ResponseCode.成功, "可注册", "0 " + _userName);
            }




        }

        private int UserInfoGetCount(string username)
        {
            //return Convert.ToInt32(SearchValue("select count(id) from userinfo where username='" + username + "'"));
            return username == "admin" ? 1 : 0;
        }

    }

}

添加ApiTools类:

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

namespace Web_api实践
{
    public class ApiTools
    {
        private string msgModel = "{{\"code\":{0},\"message\":\"{1}\",\"result\":{2}}}";
        public ApiTools()
        {
        }
        public HttpResponseMessage MsgFormat(ResponseCode code, string explanation, string result)
        {
            string r = @"^(\-|\+)?\d+(\.\d+)?$";
            string json = string.Empty;
            if (Regex.IsMatch(result, r) || result.ToLower() == "true" || result.ToLower() == "false" || result == "[]" || result.Contains('{'))
            {
                json = string.Format(msgModel, (int)code, explanation, result);
            }
            else
            {
                if (result.Contains('"'))
                {
                    json = string.Format(msgModel, (int)code, explanation, result);
                }
                else
                {
                    json = string.Format(msgModel, (int)code, explanation, "\"" + result + "\"");
                }
            }
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }

        public enum ResponseCode
        {
            操作失败 = 00000,
            成功 = 10200,
        }

    }
}

第四步:调试
下载一个非常实用的接口测试软件工具:
https://www.getpostman.com

在这里插入图片描述

执行刚才vs中的代码,将网址复制到postman中,可以显示是否成功请求。
可以在 key -value 添加数据:例如username 张三 ,下边则会显示相应的数据(如果请求成功的话)。
在这里插入图片描述

关于路由:
在这里插入图片描述

http://localhost:9529/api/UserInfo/CheckUserName?_userName=张三

其中:api默认,在网址中首先输入api,
UserInfo 对应controller
CheckUserName 对应action
_userName=张三 对应id

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓味少女vv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值