本文初步介紹如何簡單建立一個ASP.NET Web Api 程序。html
Web Api 顧名思義就是一個Api接口,客戶端可調用此接口進行業務操做。此類應用與 ASP.NET web服務(即便用擴展名.asmx的web服務文件)有必定的類似之處,又有大不一樣,web
ASP.NET Web Api 主要是基於ASP.NET MVC 框架。json
廢話少說,如今開始。api
我用的開發工具是Visul studio 2015。瀏覽器
1. 建立ASP.NET Web Api 項目。框架
改項目名稱爲DRMWebAPI,可得以下項目結構。工具
至此,API項目建立完畢。下面,咱們添加新的Controller。在Controllers文件夾下添加NewdealerController.cs文件,以下圖:post
添加完成以後,我想用post方法傳送參數的方式來調用這個接口。因此,我添加了一個類庫DRMModel,以下圖。開發工具
類庫中有一個主類 NewDealer,主類NewDealer 包含Dealer,Contact,RDaddress,HouseAddress,SendAddress五個類。ui
各個類代碼以下:
Dealer.cs
public class Dealer { public string RecruiterID { get; set; } public string Recruiter { get; set; } public string NationalID { get; set; } public string dealer_name { get; set; } public string sex { get; set; } public string BirthYear { get; set; } public string BirthMonth { get; set; } public string BirthDay { get; set; } }
Contact.cs
public class Contact { public string nightphonezonecode { get; set; } public string nightphone { get; set; } public string dayphonezonecode { get; set; } public string dayphone { get; set; } public string dayphoneext { get; set; } public string mobilephone { get; set; } public string email { get; set; } public string occupation { get; set; } public string occupation_sel { get; set; } public string RecommenderID { get; set; } public string RecommenderNationalID { get; set; } public string Recommender { get; set; } public string EINVOICE { get; set; } public string DONATEINVOICE { get; set; } }
RDaddress.cs
public class RDaddress { public string OLDMAILNO1 { get; set; } public string OLDADDRESS1 { get; set; } }
HouseAddress.cs
public class HouseAddress { public string selzipcode { get; set; } public string selprovince { get; set; } public string selcity { get; set; } public string seladdr { get; set; } public string sellane { get; set; } public string selalley { get; set; } public string selno { get; set; } public string selfl { get; set; } public string selroom { get; set; } public string selroomnum { get; set; } public string selrecipient { get; set; } }
SendAddress.cs
public class SendAddress { public string selzipcode1 { get; set; } public string selprovince1 { get; set; } public string selcity1 { get; set; } public string seladdr1 { get; set; } public string sellane1 { get; set; } public string selalley1 { get; set; } public string selno1 { get; set; } public string selfl1 { get; set; } public string selroom1 { get; set; } public string selroomnum1 { get; set; } public string selrecipient1 { get; set; } }
構造完畢以後,在NewdealerController.cs文件添加以下代碼:
using System.Web; using System.Web.Mvc; using System.Net.Http; using System.Web.Http; using System.Text; namespace DRMWebAPI.Controllers { public class NewdealerController : ApiController { //post public string Add([FromBody]DRMModel.NewDealer newDealer) { return newDealer.Dealer.dealer_name; } } }
在這裏要說明一下,調用ASP.NET Web API 接口可傳json格式的數據,API會自動把json數據轉換爲C#類型,前提是json格式要正確。
分析NewdealerController中的Add方法,可知,當咱們傳DRMModel.NewDealer json格式數據時,接口會返回newDealer.Dealer.dealer_name。
好了,那麼怎麼訪問這個接口呢?
咱們能夠查看App_Start 下的 WebApiConfig.cs 文件。
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DRMWebAPI { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
此文件定義了API的訪問路徑,如上所示,訪問NewdealerController中的Add方法的路徑爲 (域名地址/IP)/api/Newdealer/add。
下面,咱們嘗試調用這個接口,我在HomeController.cs文件中加入以下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Net; using System.Text; using System.IO; namespace DRMWebAPI.Controllers { public class HomeController : Controller { public ActionResult Index() { string postdata = "{\"Dealer\":{\"RecruiterID\":\"1\",\"Recruiter\":\"2\",\"NationalID\":\"2\",\"dealer_name\":\"啊哈哈哈\",\"sex\":\"1\",\"BirthYear\":\"1\",\"BirthMonth\":\"1\",\"BirthDay\":\"1\"},\"Contact\":{\"nightphonezonecode\":\"1\",\"nightphone\":\"1\",\"dayphonezonecode\":\"1\",\"dayphone\":\"1\",\"dayphoneext\":\"1\",\"mobilephone\":\"1\",\"email\":\"1\",\"occupation\":\"1\",\"occupation_sel\":\"1\",\"RecommenderID\":\"1\",\"RecommenderNationalID\":\"1\",\"Recommender\":\"1\",\"EINVOICE\":\"1\",\"DONATEINVOICE\":\"1\"},\"RDaddress\":{\"OLDMAILNO1\":\"1\",\"OLDADDRESS1\":\"1\"},\"HouseAddress\":{\"selzipcode\":\"1\",\"selprovince\":\"1\",\"selcity\":\"1\",\"seladdr\":\"1\",\"sellane\":\"1\",\"selalley\":\"1\",\"selno\":\"1\",\"selfl\":\"1\",\"selroom\":\"1\",\"selroomnum\":\"1\",\"selrecipient\":\"1\"},\"SendAddress\":{\"selzipcode1\":\"1\",\"selprovince1\":\"1\",\"selcity1\":\"1\",\"seladdr1\":\"1\",\"sellane1\":\"1\",\"selalley1\":\"1\",\"selno1\":\"1\",\"selfl1\":\"1\",\"selroom1\":\"1\",\"selroomnum1\":\"1\",\"selrecipient1\":\"1\"}}"; string pageHtml = ""; var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:55706//api/Newdealer/add"); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(postdata); streamWriter.Flush(); streamWriter.Close(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { pageHtml = streamReader.ReadToEnd(); } ViewData["pram1"] = pageHtml; return View(); } } }
代碼是模擬瀏覽器的Post提交數據,提交到http://localhost:55706//api/Newdealer/add 的地址裏。
其中postdata是我本身定義的NewDealer 類的json格式數據。至於類與json數據如何轉換,請自行百度。
而後,我在View文件夾添加Home文件夾,再添加Index.aspx文件。
添加代碼:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <%= ViewData["pram1"] %> </div> </body> </html>
根據以上,當我打開Home/Index時,頁面應該會輸出postdata這個json格式數據中的\"dealer_name\":\"啊哈哈哈\",即頁面會輸出: 啊哈哈哈
以下圖:
Demo下載地址:http://files.cnblogs.com/files/JinvidLiang/DRMWebAPI.zip