1,新建ASP.NET Web應用程序
2,選擇“空” 核心引用“WebApi”打勾
3,Web應用的啟動文件是Global.asax.cs
4,WebApi我們主要使用Controllers和Models
5,在Controllers添加類
5.1,在Controllers添加2個類
另外一個類
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using _002_新建WebApi.Models; namespace _002_新建WebApi.Controllers { //這個Controller返回Model數據 public class ModelDataApiController:ApiController { public CustomModel GetCustomData() { var model = new CustomModel() { Id="ModelOne", Count = 1000 }; return model; } } }
6,在Models中添加一個類
6.1,添加引用
7,這個時候啟動項目,Get請求的方法會默認到達Controller層的以“Get”開頭的方法中
8,這時候我們發現出了能獲取Controller的Get方法以外,訪問其他方法都報錯
解決辦法:
8.1,在WebApiConfig中添加Action層
8,2,方法要以Get開頭或者指定為Get方法
9,現在可以訪問方法了