2012-08-23 40 views
4

您好,我嘗試使用Ajax的Web API控制器嘗試POST/PUT/DELETE對象時遇到問題。無論我嘗試什麼,我的Hello對象都得到Post/Put爲id = 0,hello = nullWeb API控制器將不能在ASP.NET Web窗體中使用POST/PUT/DELETE

我在做什麼會導致這種行爲?

//ServicingController.cs 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Web.Http; 

namespace ServicingWebApi.Api 
{ 
    public class ServicingController : ApiController 
    { 
     public class Hello 
     { 
      public int id { get; set; } 
      public string hello { get; set; } 

      public override string ToString() 
      { 
       return string.Format("id: {0}, hello {1}", id, hello); 
      } 
     } 

     // GET api/<controller> 
     public IEnumerable<string> Get() 
     { 
      return new[] { "value1", "value2" }; 
     } 

     // GET api/<controller>/5 
     public string Get(int id) 
     { 
      return "value"; 
     } 

     // POST api/<controller> 
     public void Post(Hello stuff) 
     { 
      Debug.Write(stuff.ToString()); 
     } 

     // PUT api/<controller>/5 
     public void Put(int id, Hello stuff) 
     { 
      Debug.Write(stuff.ToString()); 
     } 

     // DELETE api/<controller>/5 
     public void Delete(int id) 
     { 
      Debug.Write(string.Format("Deleted id: {0}", id)); 
     } 
    } 
} 

ServicingPage.aspx

<%@ Page AutoEventWireup="true" CodeBehind="ServicingPage.aspx.cs" Inherits="ServicingWebApi.ServicingPage" Language="C#" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 

     //Get 
     $('#get').click(function (e) { 
      e.preventDefault(); 
      $.getJSON("api/Servicing"); 
     }); 

     //Get Single 
     $('#getsingle').click(function (e) { 
      e.preventDefault(); 
      $.getJSON("api/Servicing/" + 1); 
     }); 

     //Post 
     $('#post').click(function (e) { 
      e.preventDefault(); 
      var data = JSON.stringify({ 
      stuff: { 
       id: 1, 
       hello: "World" 
      } 
      }); 
      $.ajax({ 
      url: "api/Servicing", 
      type: "POST", 
      contentType: "application/json;charset=UTF-8", 
      data: data 
      }); 
     }); 

     //Put 
     $('#put').click(function (e) { 
      e.preventDefault(); 
      var data = JSON.stringify({ 
      stuff: { 
       id: 1, 
       hello: "World" 
      } 
      }); 
      $.ajax({ 
      url: "api/Servicing/" + 1, 
      type: "PUT", 
      contentType: "application/json;charset=UTF-8", 
      data: data 
      }); 
     }); 

     //Delete 
     $('#delete').click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
      url: "api/Servicing/" + 1, 
      type: "DELETE", 
      contentType: "application/json;charset=UTF-8", 
      }); 
     }); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <button id="get">Get Hello</button> 
     <button id="getsingle">Get a Hello</button> 
     <button id="post">Post Hello</button> 
     <button id="put">Put Hello</button> 
     <button id="delete">Delete Hello</button> 
    </div> 
    </form> 
</body> 
</html> 

的Global.asax

using System; 
using System.Web; 
using System.Web.Http; 
using System.Web.Routing; 

namespace ServicingWebApi 
{ 
    public class Global : HttpApplication 
    { 

     protected void Application_Start(object sender, EventArgs e) 
     { 
      RouteTable.Routes.MapHttpRoute(
       "DefaultApi", 
       "api/{controller}/{id}", 
       new {id = RouteParameter.Optional} 
       ); 
     } 

     protected void Session_Start(object sender, EventArgs e) 
     { 

     } 

     protected void Application_BeginRequest(object sender, EventArgs e) 
     { 

     } 

     protected void Application_AuthenticateRequest(object sender, EventArgs e) 
     { 

     } 

     protected void Application_Error(object sender, EventArgs e) 
     { 

     } 

     protected void Session_End(object sender, EventArgs e) 
     { 

     } 

     protected void Application_End(object sender, EventArgs e) 
     { 

     } 
    } 
} 

packages.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="AspNetWebApi" version="4.0.20710.0" /> 
    <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" /> 
    <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" /> 
    <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" /> 
    <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" /> 
    <package id="Microsoft.Net.Http" version="2.0.20710.0" /> 
    <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" /> 
    <package id="Newtonsoft.Json" version="4.5.8" /> 
    <package id="System.Json" version="4.0.20126.16343" /> 
</packages> 
+0

你不應該需要對你的json進行串聯化。 – jrummell

+0

@jrummell我試過了,但控制器會拋出一個反序列化錯誤。 – Romoku

回答

4

嘗試用這種數據格式

var data = JSON.stringify({ id: 1, hello: "World" }); 
+1

只是一個澄清,默認的模型聯編程序將使用確切的反序列化的JSON對象,所以它試圖映射到具有stuff參數的對象。由於你沒有,它不會正確映射。實現它作爲Claudio已經可以工作 –

+0

@ClaudioRedit @AndrewBurgess對於任何想知道的問題,當你POST一個ASMX'[Web方法]時,例如('public static void Post(Hello stuff)'),你需要圍繞你的JSON數據來匹配參數的名字,例如'stuff'或者它不會反序列化。 – Romoku

+0

@Romoku我遇到過與WCF和JSON相同的事情。 MVC更寬容一點。 –

相關問題