WebApi实质上是进行数据处理和返回的一个服务,在前后分离的工程中起着关键性的桥梁作用
新建WebApi工程
打开visual studio,ctrl+shift+N
新建WebApi项目


新建一个空项目,先不要勾选 为HTTPS位置

在vs给我们创建的工程项目中,HomeController
实际上是MVC模块的Controller

而ApiController
才是我们所需要进行业务开发的地方

F10运行项目,会看到WebApi的Home Page页面,里面展示了一系列的向导

在浏览器地址输入http://localhost:61035/api/result
,会发现,页面返回了一个XML

但是我们在实际的业务开发中,都是用的JSON格式的数据,那么我们用postman访问这个地址试试

会发现接口返回的是JSON格式的数据
这个返回的数据是怎么实现的呢,我们打开工程的Controllers/ValuesController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApi.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
//这里可以看到我们在请求地址的时候,实际上触发了这个行为(action)
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5 这里传入的id参数,
//也就是说如果我们请求http://localhost:61035/api/values/1
//或者http://localhost:61035/api/values/1地址的时候
//接口会返回一个"value"的字符串
public string Get(int id)
{
return "value";
}
}
}
那么http://localhost:61035/api/values/
中的api这又是从何而来的呢?
我们打开APP_Start/WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
// Web API 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
//name在一个项目中不允许重复
name: "DefaultApi",
//api实际上就是访问controller的前一级,如果我们把api改成aaa,那么我们访问api/后面的任何接口都是无效的
//{controller}controller实际上就是Controller文件夹下面的XXXXController类文件的前缀
//{id}实际上就是请求controller里面类方法所传递的参数,默认是可选的
routeTemplate: "api/{controller}/{id}", //
defaults: new { id = RouteParameter.Optional }
);
}
}
}
通过上面的分析,我们可以发现,ValuesController
上通过定义不同的方法,并且及进行方法的重载,实现了有参、无参、参数的Get
请求
通过对此分析,我们模拟一个自定义的Controller
来实现接口的数据请求
首先在Models
中新建一个Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApi.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
}
然后在Controller
右键-添加-控制器-Web Api-Web Api2 控制器-空

命名为StudentsController
,注意Controller
一定不要省略,否则接口无法生效

在StudentsController
编写如下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class StudentsController : ApiController
{
public Student Get()
{
return new Student(){Id = 1,Name = "kysin"}
}
}
}
可以看到返回了一个对象,而数组的内容就是我们return出去的这个Student对象
用postman测试请求

如果想返回一个数组怎么办呢
编写如下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class StudentsController : ApiController
{
public List<Student> Get()
{
return new List<Student>()
{
new Student() { Id = 1, Name = "kysin1" },
new Student() { Id = 2, Name = "kysin2" },
new Student() { Id = 3, Name = "kysin3" },
new Student() { Id = 4, Name = "kysin4" },
new Student() { Id = 5, Name = "kysin5" }
};
}
}

用postman测试请求可以看到返回了一个数组,而数组的内容就是我们return出去的这个List集合
继续编写StudentsController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class StudentsController : ApiController
{
public string GetName(string name)
{
return "你传入的参数是" + name;
}
public string GetAddress(string address)
{
return "你传入的地址是" + address;
}
}
}
分别用postman测试


如果我们去掉这个Get前缀呢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class StudentsController : ApiController
{
public string Address(string address)
{
return "你传入的地址是" + address;
}
}
}

因此可以看到,WebApi通过Get前缀的方式进行http请求的类型识别,这种必须以get、post、delet、put等前缀方式请求格式为restful
方式
参数传递分析
Get的参数传递
在http中,以get方式请求的参数实际上是通过url后面拼接参数的键名=键值
来完成传递的,那么在webapi中就对应请求的参数就是通过方法的参数来自动进行获取
但是对于body而言,数据既可以放在url里面拼接,也可以放在body中进行传递,那么在webapi中我们应该如何进行代码编写呢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class StudentsController : ApiController
{
public void Post(Student stu)
{
return "成功"
}
}
}

网友评论