||
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>
注:使用瀏覽器測試 Web Api 并不是一個好主意,它不太好測試其它諸如 POST/PUT/DELETE 方法。建議使用大名頂頂?shù)?nbsp;Fiddler 神器,可以構(gòu)造參數(shù)和選擇方法。呵呵,個人認(rèn)為最好的測試辦法是編寫一個網(wǎng)頁,在網(wǎng)頁中使用 jQuery Ajax 編寫一段小程序在 Chrome瀏覽器/360極速瀏覽器中進行測試,因為 Chrome 的調(diào)試工具箱非常強大。
注:約定俗成,控制器中方法名稱以“Get”開頭的方法默認(rèn)為“HttpGet”方法,以此類推,“Post”開頭的方法默認(rèn)為“HttpPost”方法...,它們不必注明[HttpGet]/[HttpPost]...。否則,必須方法定義的上一行用[HttpGet]/[HttpPost]...注明方法。呵呵,ASP.NET Web Api 和 Mvc 中有很多約定俗成規(guī)定。
|站長郵箱|小黑屋|手機版|Office中國/Access中國
( 粵ICP備10043721號-1 )
GMT+8, 2025-7-13 09:08 , Processed in 0.256191 second(s), 17 queries .
Powered by Discuz! X3.3
© 2001-2017 Comsenz Inc.