private static string GetXmlCommentsPath() { return System.String.Format(@"{0}\bin\WebApiDemo.xml", System.AppDomain.CurrentDomain.BaseDirectory); }
/// <summary> /// 測試的controller /// </summary> public class TestController : ApiController { /// <summary> /// get列表 /// </summary> /// <returns></returns> public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } /// <summary> /// get請求 /// </summary> /// <param name="id"></param> /// <returns></returns> public string Get(int id) { return "value"; } /// <summary> /// post請求 /// </summary> /// <param name="token"></param> /// <param name="model"></param> /// <returns></returns> public HttpResponseMessage Post(string token, [FromBody]UserModel model) { CookieHelper.SetUserCookie(model); string strResult = "{\"UserName\": \"" + model.UserName + "\", \"Pwd\": \"" + model.Pwd + "\"}"; return new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(strResult, Encoding.UTF8, "text/json") }; } }
get請求
post請求
當請求的content-type是application/json的時候,請求的參數是json字符串,後臺能夠正常接收參數
當請求的content-type是application/x-www-form-urlencoded的時候,請求的參數是以下的形式,後臺就能夠正常接收參數
web