0

这是我的控制器;

public class ProductionStateController : ApiController
    {
        private readonly IFranchiseService _franchiseService;
        public ProductionStateController(IFranchiseService franchiseService)
        {
            _franchiseService = franchiseService;
        }

        [DataContext]
        public string PutProductionState(int id, FranchiseProductionStates state)
        {
          _franchiseService.ChangeProductionState(id, state);

           var redirectToUrl = "List";

           return redirectToUrl;
        }
    }

我的ajax调用;

self.selectState = function (value) {
                $.ajax({
                    url: "/api/ProductionState",
                    type: 'PUT',
                    contentType: 'application/json',
                    data: "id=3&state=Pending",
                    success: function (data) {
                        alert('Load was performed.');
                    }
                });
            };

我的路线;

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

我收到一个404 File not found错误。

如果我将方法替换为POST.

如果我让它一切GET正常。

我在这里遗漏了一些东西。任何帮助将不胜感激。

4

2 回答 2

1

Web api 框架匹配以 http 动词开头的操作方法。所以PutProductionState可以作为一个名字。

我能够完成这项工作。问题如下:action方法的第二个参数要标记[FromBody]属性:

public string PutProductionState(int id, [FromBody] FranchiseProductionStates state)
        {
             _franchiseService.ChangeProductionState(id, state);

            var redirectToUrl = "List";

            return redirectToUrl;
        }

ajax 调用应该是这样的:

self.selectState = function (value) {
                $.ajax({
                    url: "/api/ProductionState/3",
                    type: 'PUT',
                    contentType: 'application/json',
                    data: "'Pending'",
                    success: function (data) {
                        alert('Load was performed.');
                    }
                });
            };

请注意添加到 url 和字符串化数据的 id 参数。

谢谢!

于 2013-08-13T12:55:50.497 回答
0
<script>
function CallData(ids) {
    debugger;
    if (ids != null) {
        $.ajax({
            url: "EVENT To Call (Which is in Controller)",
            data: {
                SelId: $("#Control").val()
            },
            dataType: "json",
            type: "POST",
            error: function () {
                alert("Somehitng went wrong..");
            },
            success: function (data) {
                if (data == "") {
                    //Do Your tuff
                }
            }
        });
    }
}

//在控制器中

[HttpPost]
public ActionResult EVENT To Call (Which is in Controller) (int ids)
{
  //Do Your Stuff
 return Json(Your Object, JsonRequestBehavior.AllowGet);
}
于 2013-10-25T10:58:25.287 回答