2013-03-03 123 views
2

讓我得到「404未找到」做一個Ajax調用, 也許,我不知道如何路由作品...apicontroller不從阿賈克斯

function ApproveAlert(ID) { 
    $.ajaxPost('/api/DeviceApi/ApproveAlertNew', { 'ID': ID }, function (data) { 
     ... get error "404 Not Found" 
    }, null); 
} 
我mvc4 C#應用程序

我有狂勝配置:

RouteTable.Routes.MapHttpRoute(
       name: "defaultapiaction", 
       routeTemplate: "api/{controller}/{action}" 
      ); 
      RouteTable.Routes.MapHttpRoute(
       name: "defaultapiid", 
       routeTemplate: "api/{controller}/{action}/{id}" 
      ); 

和apicontroller:

public class DeviceApiController : ApiController 
    { 
     // 
     // GET: /DeviceApi/ 
     [HttpPost] 
     public bool ApproveAlertNew(int ID) 
     { 
      ..do 
     } 
+0

嘗試使用'$ .post('/ api/DeviceApi/ApproveAlertNew?ID ='+ ID,{},函數(數據)' – nemesv 2013-03-03 16:13:18

+0

我想要發佈數據 – 2013-03-03 17:11:07

回答

0

的Web API控制器不相同SENS使用 「操作」 e MVC控制器。 Web API控制器也不會真正使用[HttpPost],[HttpGet]屬性。他們根據ApiControllers內部方法的名稱分派請求。我建議閱讀關於Web API與MVC差異的更多內容,因爲它們很相似,但有時很難啓動和運行......

下面是我爲測試製作的Web API的一些非常通用的示例。我沒有JavaScript發佈到這個API,因爲我是從.NET WPF應用發佈的。你會張貼到 「/重要」 NOT 「/重要/郵報」 希望這將讓你在正確的軌道上......

WebAPIConfig.cs(路線):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 

namespace ArrayTest.WebAPI 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
     } 
    } 
} 

API控制器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using ArrayTest.Models; 
using System.Threading; 

namespace ArrayTest.WebAPI.Controllers 
{ 
    public class ImportantController : ApiController 
    { 
     // POST api/important 
     public HttpResponseMessage Post(ImportantList values) 
     { 
      //manipulate values received from client 
      for (int i = 0; i < values.ImportantIDs.Count; i++) 
      { 
       values.ImportantIDs[i] = values.ImportantIDs[i] * 2; 
      } 

      //perhaps save to database, send emails, etc... here. 

      Thread.Sleep(5000); //simulate important work 

      //in my case I am changing values and sending the values back here. 
      return Request.CreateResponse(HttpStatusCode.Created, values); 
     } 

    } 
} 

型號:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ArrayTest.Models 
{ 
    public class ImportantList 
    { 
     public List<int> ImportantIDs { get; set; } 
    } 
} 
0

你可以嘗試用:

function ApproveAlert(ID) { 
    $.ajax({ 
     type: 'POST', 
     url: "/api/DeviceApi/ApproveAlertNew", 
     data: { 
      ID: ID 
     }, 
     success: function (data) { 
      //Handle your success 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      //Handle error 
     } 
    }); 
}