||
public static class WebApiConfig{public static void Register(HttpConfiguration config){// Web API routesconfig.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional });}}
public class ValueController : ApiController{// GET api/valuepublic IEnumerable<string> Get(){return new string[] { "value1", "value2" };}// GET api/value/5public string Get(int id){return "value";}// POST api/valuepublic void Post([FromBody]string value){}// PUT api/value/5public void Put(int id, [FromBody]string value){}// DELETE api/value/5public void Delete(int id){}}
This XML file does not appear to have any style information associated with it. The document tree is shown below.<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><string>value1</string><string>value2</string></ArrayOfstring>
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">value</string>
注:使用瀏覽器測(cè)試 Web Api 并不是一個(gè)好主意,它不太好測(cè)試其它諸如 POST/PUT/DELETE 方法。建議使用大名頂頂?shù)?nbsp;Fiddler 神器,可以構(gòu)造參數(shù)和選擇方法。呵呵,個(gè)人認(rèn)為最好的測(cè)試辦法是編寫(xiě)一個(gè)網(wǎng)頁(yè),在網(wǎng)頁(yè)中使用 jQuery Ajax 編寫(xiě)一段小程序在 Chrome瀏覽器/360極速瀏覽器中進(jìn)行測(cè)試,因?yàn)?Chrome 的調(diào)試工具箱非常強(qiáng)大。
注:約定俗成,控制器中方法名稱以“Get”開(kāi)頭的方法默認(rèn)為“HttpGet”方法,以此類(lèi)推,“Post”開(kāi)頭的方法默認(rèn)為“HttpPost”方法...,它們不必注明[HttpGet]/[HttpPost]...。否則,必須方法定義的上一行用[HttpGet]/[HttpPost]...注明方法。呵呵,ASP.NET Web Api 和 Mvc 中有很多約定俗成規(guī)定。
|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )
GMT+8, 2024-10-23 08:27 , Processed in 0.065683 second(s), 17 queries .
Powered by Discuz! X3.3
© 2001-2017 Comsenz Inc.