Asp.Net MVC part6 WebAPI

來源:互聯網
上載者:User

標籤:輸出   dia   http   url   擷取   調用   span   執行   web   

 

兩種web服務
SOAP風格:基於方法,產品是WebService
REST風格:基於資源,產品是WebAPI
可以返回json、xml類型的資料
對於資料的增、刪、改、查,提供相對的資源操作,按照請求的類型進行相應處理,主要包括Get(查)、Post(增)、Put(改)、Delete(刪),這些都是http協議支援的請求方式
請求方式:根據路由規則請求

在APIController中定義crud的方法,名稱可以自訂,如果對應相應的資源操作,可以使用特性約束
主要的特性包括
HttpGet
HttpPost
HttpPut
HttpDelete

 

使用方式1:jquery的ajax
指定請求的資料類型: contentType: "application/json; charset=utf-8",//資料類型
主要的屬性:
type:請求方式,包括Get、Post、Put、Delete
url:請求資源,根據路由規則編寫
data:請求資料,為json格式
contentType:請求資料的類型及編碼
dataType:返回的資料類型,可以是text、json
success:成功處理的回呼函數
備忘中為修改請求的樣本
注意:使用js的非同步作業,不可以跨域訪問

var data = ‘{"UserId":"‘ + $(‘#userId‘).val() +‘","UserName":"‘ + $(‘#userName‘).val() + ‘"}‘;                $.ajax({                    type: ‘PUT‘,//請求類型。get,post,put,delete                    url: ‘api/UserInfo/‘ + $(‘#userId‘).val(),//請求地址                    data: data,//參數                    contentType: "application/json; charset=utf-8",//資料類型                    dataType: ‘text‘,//返回資料類型                    success: function (msg) {                        if (eval(msg) == ‘1‘) {                            InitData();                        }                    }                }); 

使用方式2:HttpClient對象  Crud操作的API

建立並初始化對象:    client.BaseAddress = new Uri(url);    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));讀集合:    HttpResponseMessage response = client.GetAsync(url).Result;     var userList = response.Content.ReadAsAsync<IEnumerable<資料類型>>().Result;根據編號讀對象    HttpResponseMessage response1 = client.GetAsync(url).Result;    var userInfo = response1.Content.ReadAsAsync<資料類型>().Result;增加:    HttpResponseMessage response = client.PostAsJsonAsync("api/userinfo", userInfo).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值修改:     HttpResponseMessage response = client.PutAsJsonAsync("api/userinfo/"+userInfo.UserId, userInfo).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值刪除:    HttpResponseMessage response = client.DeleteAsync("api/userinfo/" + uid).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值

原理(管道執行過程)

擷取ControllerFactory,根據上下文反射Controller對象在MvcHandler的PR方法中調用controller.Execute方法在MvcHandler的PR方法中調用ActionInvoker.InvokeAction方法當前Controller類-》ControllerBase類的Execute方法-》Controller類的ExecuteCore方法調用InvokeActionResultWithFilterActionInvoker對象為ControllerActionInvoker類的執行個體調用actionResult.ExecuteResult調用View.Render方法,將頁面渲染到輸出資料流中ViewResult類-》ViewResultBase類

 

Asp.Net MVC part6 WebAPI

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.