||
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 方法。建议使用大名顶顶的 Fiddler 神器,可以构造参数和选择方法。呵呵,个人认为最好的测试办法是编写一个网页,在网页中使用 jQuery Ajax 编写一段小程序在 Chrome浏览器/360极速浏览器中进行测试,因为 Chrome 的调试工具箱非常强大。
注:约定俗成,控制器中方法名称以“Get”开头的方法默认为“HttpGet”方法,以此类推,“Post”开头的方法默认为“HttpPost”方法...,它们不必注明[HttpGet]/[HttpPost]...。否则,必须方法定义的上一行用[HttpGet]/[HttpPost]...注明方法。呵呵,ASP.NET Web Api 和 Mvc 中有很多约定俗成规定。
|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )
GMT+8, 2024-12-24 00:15 , Processed in 0.057551 second(s), 17 queries .
Powered by Discuz! X3.3
© 2001-2017 Comsenz Inc.