上篇:webapi快速框架搭建-建立项目(一)html
空项目默认是没有webapi相关的dll,要本身去nuget里安装。web
using System; using System.Web.Http; namespace webapi { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { #region webapi的相关配置 // GlobalConfiguration在Microsoft.AspNet.WebApi.Core里,用nuget添加Microsoft.AspNet.WebApi GlobalConfiguration.Configure(config => { // Web API 路由 config.MapHttpAttributeRoutes();//启用webapi的属性路由 // 配置webapi的默认路由模板 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }); #endregion } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
打开win10的iis管理器,建立一个网站,以下图。express
固然也能够用vs自带的iis express,直接运行(按f5)就能够了。在开发时可用iis express快速的调试,但部署时就得用上iis了。api
我在开发时习惯部署到iis上,这样开发时只要编译程序后就直接能够用postman来访问webapi接口是否正常,不用每次都启动iis express,这样速度有点慢,更方便的是能够用vs的调试--》附加到进程,来对已经发布的webapi接口网站进行调试,甚至只以远程调试服务器上的iis网站来排查问题。服务器
建立webapi接口控制类mvc
using System.Web.Http; namespace webapi.example { public class TestController : ApiController { public IHttpActionResult Get() { return Ok("this is TestController.Get()"); } } }
编译后用postman访问接口的地址:http://localhost:101/api/testapp
webapi的路由和控制器在网站有不少教程,可自行学习,我我的建议看webapi的官网教程就好了,有中文版的,已经说的很清楚了,也更权威框架
webapi官方文档:webapi路由post