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

前言

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

正文

新建一个asp.net 的项目,然后选择 空 web api 项目。
C#搭建一个简单的web api项目_第1张图片

建成以后,可以看出,和mvc非常的相似。
C#搭建一个简单的web api项目_第2张图片
第一步:修改路由
在 App_start 中,web api 是没有action 的路由,这一点和mvc有些不同,为了方便,我们修改路由和mvc一样~(添加 action)
C#搭建一个简单的web api项目_第3张图片

第二步:添加一个控制器
C#搭建一个简单的web api项目_第4张图片
第三步:编码

在控制器中:添加一个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

C#搭建一个简单的web api项目_第5张图片

执行刚才vs中的代码,将网址复制到postman中,可以显示是否成功请求。
可以在 key -value 添加数据:例如username 张三 ,下边则会显示相应的数据(如果请求成功的话)。
C#搭建一个简单的web api项目_第6张图片

关于路由:
C#搭建一个简单的web api项目_第7张图片

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

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

你可能感兴趣的:(B/S,——ASP.NET)