WEBAPI 的简单示例
一、webapi
1.在webapiconfig中移除xml的返回格式,返回格式就自动使用Json格式
config.Formatters.Remove(config.Formatters.XmlFormatter);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace WebApplication2 { public static class WebApiConfig { public static void Register(HttpConfiguration config) { //移除XML格式,返回值自动就变成json格式 config.Formatters.Remove(config.Formatters.XmlFormatter); // Web API 配置和服务 //Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi" , routeTemplate: "api/{controller}/{id}" , defaults: new { id = RouteParameter.Optional } ); } } } |
2.创建一个pig的控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace WebApplication2.Controllers { public class PigController : ApiController { //GET: api/Pig //public IEnumerable<string> Get() //{ // return new string[] { "大pig", "value2" }; //} public Pig Get() { Pig pig = new Pig() { Age = 1, Name = "大黄狗" }; return pig; } //GET: api/Pig/5 public string Get( int id) { return "value" ; } //POST: api/Pig public void Post([FromBody] string value) { } //PUT: api/Pig/5 public void Put( int id, [FromBody] string value) { } //DELETE: api/Pig/5 public void Delete( int id) { } } } |
3.发布webapi网站,url=http://localhost:34050/api/Pig
二、客户端调用
新建一个网站,简单一点,就用web窗体,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { using System.Net; public partial class Pig : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { //1.0请求的webapi的url:http://localhost:34050/api/Pig //1.0构造一个制定url请求对象 WebRequest request = WebRequest.Create( "http://localhost:34050/api/Pig" ); //2.0指定请求的方法为get request.Method = "Get" ; //3.0发出请求获取相应对象 WebResponse response = request.GetResponse(); //4.0获取相应报文体中的数据 System.IO.Stream st = response.GetResponseStream(); //5.0将st转换成字符串 string resStr = string .Empty; using (System.IO.StreamReader sr = new System.IO.StreamReader(st)) { //从当前流的开始位置读至结束位置 resStr = sr.ReadToEnd(); //{"Age":1,"Name":"大黄狗"} } //6.0将结果绑定到Grid上 //将json格式的字符串反序列化成集合 System.Web.Script.Serialization.JavaScriptSerializer jsoner = new System.Web.Script.Serialization.JavaScriptSerializer(); ResultPig rpig= jsoner.Deserialize<ResultPig>(resStr); //如果结果是[{},{}] //jsoner.Deserialize<List<ResultPig>>(resStr); List<ResultPig> list = new List<ResultPig>() { rpig }; GridView1.DataSource = list; GridView1.DataBind(); //Response.Write(resStr); } } public class ResultPig { public int Age { get ; set ; } public string Name { get ; set ; } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤