简体   繁体   English

C#Web API无法用作POST类型

[英]c# web api not working as POST Type

Below is my API structure 下面是我的API结构

 public class CustomController : ApiController
{
   [HttpPost]
    public DataSet POSTM(string StaffCode, string bytearray, string Reason)
   {
    //Some Business logic Here
   }
}

And its route 及其路线

 config.Routes.MapHttpRoute(
                name: "DefaultApi2",
                routeTemplate: "api/{controller}/{staffCode}/{bytearray}/{Reason}",
                defaults: new {
                    staffCode = RouteParameter.Optional,
                    bytearray = RouteParameter.Optional,
                    Reason = RouteParameter.Optional

                }
            );

I am try to invoke it from jquery ajax as 我尝试从jquery ajax调用它

var data = {
            staffCode: staffCode_,
            bytearray: bytearray_,
            Reason: Reason_,
        };

        $.ajax(
            { url: 'http://localhost:59118/api/Custom/', 
             method : 'POST', 
             dataType: 'JSON', 
             data:data, 
             success: function (d) 
             { 
                 alert("Saved Successfully"); 

             }, 
             error: function (e) 
             { 
                 alert("Error please try again"); 
             } 
            });

After invoking it I am getting 500 Internal Server Error in console Log 调用后,我在控制台日志中收到500 Internal Server Error

But if I changed [HttpPost] to [HttpGet] than it works perfectly fine 但是,如果我将[HttpPost]更改为[HttpGet],则效果很好

Why it is not working as POST ? 为什么它不能作为POST工作?

You are using POST and sending data in body then you need to bind model like this 您正在使用POST并在body发送数据,则需要像这样绑定模型

[HttpPost]
public DataSet POSTM(MyModel model)
{
  //Some Business logic Here
}

and MyModel will be a class MyModel将是一个类

public class MyModel 
{
  public string staffCode { get; set; }
  public string bytearray { get; set; }
  public string reason { get; set; }
}

leave web api config route as it is by default 保留默认的Web api配置路由

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

Then try call it like this 然后尝试这样称呼它

    var staffCode = "staff code here";
    var bytearray = "hell byte arry";
    var Reason = "no reason";

    $.ajax(
        {
            url: 'http://localhost:51524/api/Test/POSTM/',
            method: 'post',
            data: JSON.stringify([staffCode, bytearray, Reason]),
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                alert("Saved Successfully");

            },
            error: function (e) {
                alert("Error please try again");
            }
        });

controller look like this 控制器看起来像这样

 [HttpPost]
    public DataSet POSTM(List<string> data)
    {
      //ur code here
    }

Receiving request from ajax 接收来自ajax的请求 在此处输入图片说明

While you are sending data from Ajax, 从Ajax发送数据时,

Your attribute " staffCode " doesn't match with controller method attribute " StaffCode ". 您的属性“ staffCode ”与控制器方法属性“ StaffCode ”不匹配。

Attribute names are case sensitive. 属性名称区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM