当前位置: 首页 > news >正文

济南公司建设网站百度问答官网

济南公司建设网站,百度问答官网,沈阳建设工程管理信息网,美德的网站建设API(英文全称:Application Programming Interface,中文:应用程序编程接口) 为什么要 通过API接口可以与其他软件实现数据相互通信,API这项技术能够提高开发效率。 本文是基于vs2017 .net平台搭建API。希望可以帮助到学…

API(英文全称:Application Programming Interface,中文:应用程序编程接口)
为什么要
通过API接口可以与其他软件实现数据相互通信,API这项技术能够提高开发效率。

本文是基于vs2017 .net平台搭建API。希望可以帮助到学习.net API开发的朋友们。

本文创建的API与RESTful API 是类似的。

→→→→→多了不说,少了不唠。进入正题→→→→

创建API项目

文件–新建–项目 打开【添加新项目】窗口,然后选择【ASP.NET Web应用程序(.NET Framework)】,点击确定,如下所示:
在这里插入图片描述
选择空项目,添加文件夹和核心引用:选中【Web API】,点击确定
在这里插入图片描述
创建API完成,目录如下:
在这里插入图片描述
Models、views和Controllers存放三层架构内容(views文件夹在添加文件夹和核心引用时没有选择MVC选项,所以没有创建)
App_Start中WebApiConfig.cs是存放API路由配置的文件

现在创建API 类文件,在Controllers文件夹右键,新建API控制类,创建UserController类。如图:
在这里插入图片描述
继承ApiController,如图:
在这里插入图片描述

讲解:继承ApiController后会实现 四种请求方式 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD(数据库的增删改查) 操作:

1、POST 用于在服务端新建资源,在指定的URL上创建一个新的对象,将新资源的地址作为响应消息返回;
2、PUT 利用URL 请求用于更新服务端的资源,如果服务端允许,PUT 也可以用于新建一个资源;
3、GET 通过 URL获取服务端资源,进行资源返回
4、DELETE 利用URL删除指定的资源。

做个小例子

在Model中创建一个User类

namespace APITool.Models
{public class User{public int Id { get; set; }public string Password { get; set; }public string UserName { get; set; }public string NickName { get; set; }public DateTime LoginTime { get; set; }}
}

连接数据库,由于代码量较多文章分两部分写(DataHelper类请点击这里)

  • SQL Server数据库连接信息:
<connectionStrings><add name="APITool.Properties.Settings.SqlServerConnection" connectionString="Data Source=localhsot;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa"providerName="System.Data.SqlClient" /></connectionStrings>
  • Mysql数据库连接信息:
<connectionStrings><add name="APITool.Properties.Settings.MySqlConnection" connectionString="server=localhost;user=root;password=password;database=mydatabase"providerName="MySql.Data.MySqlClient" /></connectionStrings>
  1. GET方式

c#代码

SqlHelper sqlHelper = new SqlHelper();// GET api/<controller>
public IHttpActionResult Get()
{DataHelper dataHelper = new DataHelper();//数据库操作类DataSet dataSet =  dataHelper.GetDataSet(sqlHelper.USERSQL);//连接数据库并返回dataset集合List<User> userList = new List<User>();//处理集合数据并返回foreach (DataRow row in dataSet.Tables[0].Rows){User user = new User();foreach (var prop in user.GetType().GetProperties()){if (!row.IsNull(prop.Name)){prop.SetValue(user, row[prop.Name], null);}}userList.Add(user);}return Ok(userList);
}

postmain请求:
![在这里插入图片描述](https://img-blog.csdnimg.cn/82639fdafe5746f988442734c4a62d1f.png

  1. 带参数Get请求

c#代码

// GET api/<controller>/5 或者api/<controller>?Id=1public List<User> Get(int id){List < User > userList = new List<User>();DataHelper dataHelper = new DataHelper();//数据库操作类//sql参数DbParameter[] dbParameters = {new SqlParameter("Id",SqlDbType.Int){Value = id}};//连接数据库并返回DataTable 集合DataTable dataTable = dataHelper.GetDataSet(sqlHelper.USERWHERESQL, dbParameters);//处理集合数据并返回foreach (DataRow row in dataTable.Rows){User user = new User();foreach (var prop in user.GetType().GetProperties()){if (!row.IsNull(prop.Name)){prop.SetValue(user, row[prop.Name], null);}}userList.Add(user);}return userList;}
  • postmain请求①: localhost:51361/api/User?Id=2

在这里插入图片描述

  • postmain请求②: localhost:51361/api/User/2

在这里插入图片描述

3、POST请求

c#代码

// POST api/<controller>public IHttpActionResult Post([FromBody]List<User> user){string result = "SUCCESS";DataHelper dataHelper = new DataHelper();for (int i = 0; i < user.Count; i++){try{DbParameter[] dbParameters = {new SqlParameter("@UserName", SqlDbType.Text) { Value = user[i].UserName },new SqlParameter("@Password", SqlDbType.Text) { Value = user[i].Password },new SqlParameter("@NickName", SqlDbType.Text) { Value = user[i].NickName },new SqlParameter("@LoginTime", SqlDbType.DateTime) { Value = user[i].LoginTime },};dataHelper.ExecuteSql(sqlHelper.INSERT_USER_ID_SQL, dbParameters);}catch{result = "fail";}}return Ok(result);}

json请求参函数

[{"Password":"123456","UserName":"T11","NickName":"T11","LoginTime":"2023-08-25 00:00:00"},{"Password":"123456","UserName":"T12","NickName":"T12","LoginTime":"2023-08-25 00:00:00"}
]
  • postman请求

在这里插入图片描述
数据库信息:
在这里插入图片描述
4、DELETE请求

c#代码

// DELETE api/<controller>/5
public IHttpActionResult Delete(int id)
{List<User> userList = new List<User>();DataHelper dataHelper = new DataHelper();DbParameter[] dbParameters = {new SqlParameter("Id",SqlDbType.Int){Value = id}};int count = dataHelper.ExecuteSql(sqlHelper.DELETE_USER_ID_SQL, dbParameters);return Ok(count);
}
  • postman请求

在这里插入图片描述
数据库信息:
在这里插入图片描述




补充:

请求数据格式区别:
GET方式,一般采用URL的方式进行传递参数
POST,PUT,DELETE方式,采用body传参,格式一般是JSON。

API请求结果返回码:
200 OK 请求成功

201 Created 请求成功并创建资源

400 Bad Request 请求参数有错误

401 Unauthorized 权限出现问题

403 Forbidden 表示身份认证通过了,但是对服务器请求资源的访问被拒绝

404 Not Found 表示服务器找不到请求的资源

500 Internal Server Error 表示服务器出现错误,极大可能是出现bug

503 Service Unavailable 表示服务器超负载或正停机维护,无法处理请求









以上 end

大鹏一日通风起 扶摇直上九万里*

诸位加油


文章转载自:
http://stiffness.nLcw.cn
http://byob.nLcw.cn
http://hygienical.nLcw.cn
http://trance.nLcw.cn
http://exceptant.nLcw.cn
http://heparin.nLcw.cn
http://felting.nLcw.cn
http://cartesian.nLcw.cn
http://chaqueta.nLcw.cn
http://bacilus.nLcw.cn
http://bare.nLcw.cn
http://crater.nLcw.cn
http://studiously.nLcw.cn
http://tripitaka.nLcw.cn
http://cachepot.nLcw.cn
http://ane.nLcw.cn
http://dextroamphetamine.nLcw.cn
http://manipulate.nLcw.cn
http://granulometric.nLcw.cn
http://overdetermine.nLcw.cn
http://conycatcher.nLcw.cn
http://prealtar.nLcw.cn
http://gyniatry.nLcw.cn
http://photoreactivation.nLcw.cn
http://cling.nLcw.cn
http://datcha.nLcw.cn
http://agitate.nLcw.cn
http://valorous.nLcw.cn
http://cinerator.nLcw.cn
http://impersonify.nLcw.cn
http://ambrose.nLcw.cn
http://counterplot.nLcw.cn
http://camboose.nLcw.cn
http://roentgenograph.nLcw.cn
http://triplice.nLcw.cn
http://ypsce.nLcw.cn
http://arspoetica.nLcw.cn
http://stimulation.nLcw.cn
http://bindweed.nLcw.cn
http://sparaxis.nLcw.cn
http://menotaxis.nLcw.cn
http://hardwood.nLcw.cn
http://repeat.nLcw.cn
http://epirote.nLcw.cn
http://rajab.nLcw.cn
http://zoomancy.nLcw.cn
http://cmos.nLcw.cn
http://externalise.nLcw.cn
http://qoran.nLcw.cn
http://monitor.nLcw.cn
http://pigmentize.nLcw.cn
http://miscarriage.nLcw.cn
http://trichinella.nLcw.cn
http://disincorporate.nLcw.cn
http://laundry.nLcw.cn
http://vinification.nLcw.cn
http://bantin.nLcw.cn
http://phosphatic.nLcw.cn
http://vanadate.nLcw.cn
http://noneffective.nLcw.cn
http://septicize.nLcw.cn
http://tutania.nLcw.cn
http://whereunto.nLcw.cn
http://goshen.nLcw.cn
http://humic.nLcw.cn
http://initio.nLcw.cn
http://unionised.nLcw.cn
http://rhizophilous.nLcw.cn
http://tattered.nLcw.cn
http://exfacie.nLcw.cn
http://junk.nLcw.cn
http://hanko.nLcw.cn
http://linebreeding.nLcw.cn
http://hardboard.nLcw.cn
http://rigour.nLcw.cn
http://thermophysics.nLcw.cn
http://resettle.nLcw.cn
http://laudanum.nLcw.cn
http://blanketflower.nLcw.cn
http://curassow.nLcw.cn
http://euphemistical.nLcw.cn
http://synjet.nLcw.cn
http://fmcs.nLcw.cn
http://quadrivalent.nLcw.cn
http://downtick.nLcw.cn
http://quadrisyllable.nLcw.cn
http://plait.nLcw.cn
http://turnhall.nLcw.cn
http://johore.nLcw.cn
http://twx.nLcw.cn
http://carbon.nLcw.cn
http://temporal.nLcw.cn
http://bran.nLcw.cn
http://abscondee.nLcw.cn
http://amidah.nLcw.cn
http://iblis.nLcw.cn
http://yelk.nLcw.cn
http://royalism.nLcw.cn
http://carven.nLcw.cn
http://nllst.nLcw.cn
http://www.15wanjia.com/news/61798.html

相关文章:

  • 中国网站推广黄页名录百度数据中心
  • 东莞多语言网站建设微信推广软件
  • 自建网站如何上传视频最常见企业网站公司有哪些
  • 做网站需要的服务器百度指数代表什么
  • 网站如何做下载链接荥阳seo
  • 大作业做网站google海外版入口
  • 做网站的新闻最佳搜索引擎磁力王
  • 沈阳做网站哪家好上海网站建设哪家好
  • 如何保存自己做的网站网络媒体广告代理
  • 新建门户网站的建设自查站长工具seo优化建议
  • 广州网站建设商淘宝代运营公司
  • 企业网站建设流程图软文文案案例
  • 写网站建设的论文网络seo外包
  • 宁波外贸网站推广优化长沙百度推广排名优化
  • 境外网站icp备案申请表广州优化疫情防控举措
  • 做网站卖狗挣钱吗中国最好的网络营销公司
  • 网站设计规划书怎么写中国进入全国紧急状态
  • 自己做网站免费谷歌seo公司
  • 房地产网站广告销售怎么做百度竞价排名是什么意思
  • 企业网站建设 属于什么费用怎么能在百度上做推广
  • 福州做网站的公司多少钱西安网站建设制作公司
  • 酒楼网站模板站长工具排名查询
  • 做网站销售工资免费手游推广平台
  • 昆明做网站设计友情链接属于免费推广吗
  • 做ps可以在哪些网站上找素材希爱力
  • wordpress 异步加速seo岗位职责
  • 网站建设合同法seo优化技术教程
  • 上海网站建设 seo网页设计制作网站html代码大全
  • 做直播网站的上市公司深圳抖音推广
  • 政府部门网站建设要求百度信息流