濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > ASP.NET中Web API的簡(jiǎn)單實(shí)例

ASP.NET中Web API的簡(jiǎn)單實(shí)例

熱門標(biāo)簽:遂寧市地圖標(biāo)注app 地圖定位圖標(biāo)標(biāo)注 400電話辦理哪家性價(jià)比高 濮陽(yáng)外呼電銷系統(tǒng)怎么樣 地圖標(biāo)注專業(yè)團(tuán)隊(duì) 塔城代理外呼系統(tǒng) 地圖標(biāo)注的公司有哪些 代理接電話機(jī)器人如何取消 天心智能電銷機(jī)器人

一、Web API的路由
1、在Visual Studio中新建MVC4項(xiàng)目,在App_Start目錄下有一個(gè)WebApiConfig.cs文件,這個(gè)文件中就是相應(yīng)的Web API的路由配置了。
2、Web API 框架默認(rèn)是基于 Restful 架構(gòu)模式的,與ASP.NET MVC 有區(qū)別的是,它會(huì)根據(jù) Http 請(qǐng)求的 HttpMethod(Get、Post、Put、Delete)來(lái)在Controller 中查找 Action,規(guī)則是:Action 名中是否以Get、Post 開(kāi)頭?Action 上標(biāo)記 HttpGet、HttpPost 等標(biāo)記?
3、當(dāng)然可以修改默認(rèn)的配置,讓客戶端在調(diào)用時(shí)顯式指定 action 名稱,例如

config.Routes.MapHttpRoute(
 name: "DefaultApi",
 routeTemplate: "api/{controller}/{action}/{id}",
 defaults: new { id = RouteParameter.Optional }
);

這樣,由于顯式指定了 Action 名稱,Web API 會(huì)使用該名稱來(lái)查找對(duì)應(yīng)的 Action 方法,而不再按照 HttpMethod 約定來(lái)查找對(duì)應(yīng)的 Action。
 二、ASP.NET中Web API的簡(jiǎn)單實(shí)例
 1、Get請(qǐng)求數(shù)據(jù)
(1)、定義一個(gè)UserModel 類

public class UserModel
{
 public string UserID { get; set; }
 public string UserName { get; set; }
}

(2)、添加一個(gè)Web API Controller :UserController

public class UserController : ApiController
{
 public UserModel getAdmin()
 {
  return new UserModel() { UserID = "000", UserName = "Admin" };
 } 
}

(3)、在瀏覽器訪問(wèn):api/user/getadmin (默認(rèn)返回的是XML數(shù)據(jù)模型)

(4)、AJAX請(qǐng)求這個(gè)api,指定數(shù)據(jù)格式為json 

$.ajax({
 type: 'GET',
 url: 'api/user/getadmin',
 dataType: 'json',
 success: function (data, textStatus) {
  alert(data.UserID + " | " + data.UserName);
 },
 error: function (xmlHttpRequest, textStatus, errorThrown) {
 }
});

 2、POST提交數(shù)據(jù)
(1)、UserController 里面添加一個(gè)Action

public bool add(UserModel user)
{
 return user != null;
}

(2)、頁(yè)面上添加一個(gè)button

input type="button" name="btnOK" id="btnOK" value="發(fā)送POST請(qǐng)求" />

(3)、JS post提交數(shù)據(jù)

$('#btnOK').bind('click', function () {
 //創(chuàng)建ajax請(qǐng)求,將數(shù)據(jù)發(fā)送到后臺(tái)處理
 var postData = {
  UserID: '001',
  UserName: 'QeeFee'
 };
 $.ajax({
  type: 'POST',
  url: 'api/user/add',
  data: postData,
  dataType: 'json',
  success: function (data, textStatus) {
   alert(data);
  },
  error: function (xmlHttpRequest, textStatus, errorThrown) {
  }
 });
});

以上就是ASP.NET中Web API的簡(jiǎn)單實(shí)例,還包括Web API路由介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。

您可能感興趣的文章:
  • 創(chuàng)建一個(gè)完整的ASP.NET Web API項(xiàng)目
  • ASP.NET MVC Web API HttpClient簡(jiǎn)介
  • ASP.NET Web Api 2實(shí)現(xiàn)多文件打包并下載文件的實(shí)例
  • 支持Ajax跨域訪問(wèn)ASP.NET Web Api 2(Cors)的示例教程
  • ASP.NET Web API教程 創(chuàng)建Admin視圖詳細(xì)介紹
  • ASP.NET Web API如何將注釋自動(dòng)生成幫助文檔
  • ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享
  • ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹
  • .Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的方法

標(biāo)簽:宜春 婁底 汕頭 重慶 河南 吉林 麗江 本溪

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET中Web API的簡(jiǎn)單實(shí)例》,本文關(guān)鍵詞  ASP.NET,中,Web,API,的,簡(jiǎn)單,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET中Web API的簡(jiǎn)單實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET中Web API的簡(jiǎn)單實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    安国市| 安陆市| 青州市| 宁国市| 江都市| 清丰县| 图们市| 阳高县| 喜德县| 青田县| 息烽县| 兰西县| 南江县| 玉环县| 常德市| 麻阳| 黄骅市| 姜堰市| 聊城市| 合川市| 万盛区| 阳春市| 嘉鱼县| 宾川县| 友谊县| 徐州市| 宁蒗| 宽城| 澄迈县| 涟水县| 延川县| 重庆市| 凤城市| 莎车县| 枝江市| 广平县| 茶陵县| 墨脱县| 农安县| 邳州市| 得荣县|