【问题标题】:Web Api: PUT/ POST method does not workWeb Api:PUT/POST 方法不起作用
【发布时间】:2013-08-14 22:18:46
【问题描述】:

这是我的控制器;

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 一切正常。

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

【问题讨论】:

    标签: asp.net-mvc-3 asp.net-mvc-4 asp.net-web-api


    【解决方案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参数。

    谢谢!

    【讨论】:

      【解决方案2】:
      <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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 2020-12-29
        • 2018-08-23
        • 2015-03-19
        • 2012-05-31
        • 2019-01-07
        相关资源
        最近更新 更多