webapi只post数据
<form id="form1" method="post" action="http://10.100.22.54:8095/api/BookChapters/PostBookChapter"> <input type="text" name="Number" id="txtNumber" /><br /> <input type="text" name="Title" id="txtTitle" /><br /> <input type="text" name="Pages" id="txtPages" /><br /> <input type="submit" value="提交"/> </form>
或者
$("#btnPost").on("click", function () { var u = {}; u["Number"] = 1000; u["Title"] = "wjl"; u["Pages"]=120 $.ajax({ url: "http://10.100.22.54:8095/api/BookChapters/PostBookChapter", type: "post", dataType: "json", data: u, success: function (d) { }, error: function (ex) { } }) })
using System.Collections; using System.Collections.Generic; using System.Web.Http; using WebApplication1.Models; using System.Linq; using Newtonsoft.Json; using WebApplication1.App_Start; namespace WebApplication1.Controllers { [AllowCrossSiteJson] public class BookChaptersController:ApiController { // GET api/bookchapters [HttpGet] public IEnumerable<BookChapter> Get() { return chapters; } // GET api/bookchapters/5 public BookChapter Get(int id) { return chapters.Where(c => c.Number == id).SingleOrDefault(); } // POST api/bookchapters [AcceptVerbs("POST")] [ActionName("bookchapter")] public void PostBookChapter([FromBody]BookChapter value) { chapters.Add(value); } // PUT api/bookchapters/5 public void PutBookChapter(int id, [FromBody]BookChapter value) { chapters.Remove(chapters.Where(c => c.Number == id).Single()); chapters.Add(value); } // DELETE api/bookchapters/5 public void DeleteBookChapter(int id) { chapters.Remove(chapters.Where(c => c.Number == id).Single()); } private static List<BookChapter> chapters; static BookChaptersController() { chapters = new List<BookChapter>() { new BookChapter{Number=1,Title=".Net Architecture",Pages=20}, new BookChapter{Number=2,Title=".Net ",Pages=120}, new BookChapter{Number=3,Title=".Net a",Pages=480}, new BookChapter{Number=4,Title=".Net b",Pages=200}, new BookChapter{Number=5,Title=".Net c",Pages=210}, new BookChapter{Number=17,Title=".Net d",Pages=230}, new BookChapter{Number=42,Title=".Net e",Pages=270}, new BookChapter{Number=43,Title=".Net f",Pages=320}, new BookChapter{Number=50,Title=".Net g",Pages=520} }; } } }

浙公网安备 33010602011771号