c# web api 不能作为 POST 类型工作
c# web api not working as POST Type
下面是我的API结构
public class CustomController : ApiController
{
[HttpPost]
public DataSet POSTM(string StaffCode, string bytearray, string Reason)
{
//Some Business logic Here
}
}
及其路线
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{staffCode}/{bytearray}/{Reason}",
defaults: new {
staffCode = RouteParameter.Optional,
bytearray = RouteParameter.Optional,
Reason = RouteParameter.Optional
}
);
我正在尝试从 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");
}
});
调用它后,我在控制台日志中收到 500 Internal Server Error
但是,如果我将 [HttpPost] 更改为 [HttpGet],它就可以正常工作
为什么它不像 POST 那样工作?
您正在使用 POST
并在 body
中发送数据,那么您需要像这样绑定模型
[HttpPost]
public DataSet POSTM(MyModel model)
{
//Some Business logic Here
}
和 MyModel
将是 class
public class MyModel
{
public string staffCode { get; set; }
public string bytearray { get; set; }
public string reason { get; set; }
}
保留 web api 默认配置路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后试着这样称呼它
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");
}
});
控制器看起来像这样
[HttpPost]
public DataSet POSTM(List<string> data)
{
//ur code here
}
正在接收来自 ajax 的请求
当您从 Ajax、
发送数据时
您的属性“staffCode
”与控制器方法属性“StaffCode
”不匹配。
属性名称区分大小写。
下面是我的API结构
public class CustomController : ApiController
{
[HttpPost]
public DataSet POSTM(string StaffCode, string bytearray, string Reason)
{
//Some Business logic Here
}
}
及其路线
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{staffCode}/{bytearray}/{Reason}",
defaults: new {
staffCode = RouteParameter.Optional,
bytearray = RouteParameter.Optional,
Reason = RouteParameter.Optional
}
);
我正在尝试从 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");
}
});
调用它后,我在控制台日志中收到 500 Internal Server Error
但是,如果我将 [HttpPost] 更改为 [HttpGet],它就可以正常工作
为什么它不像 POST 那样工作?
您正在使用 POST
并在 body
中发送数据,那么您需要像这样绑定模型
[HttpPost]
public DataSet POSTM(MyModel model)
{
//Some Business logic Here
}
和 MyModel
将是 class
public class MyModel
{
public string staffCode { get; set; }
public string bytearray { get; set; }
public string reason { get; set; }
}
保留 web api 默认配置路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后试着这样称呼它
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");
}
});
控制器看起来像这样
[HttpPost]
public DataSet POSTM(List<string> data)
{
//ur code here
}
正在接收来自 ajax 的请求
当您从 Ajax、
发送数据时您的属性“staffCode
”与控制器方法属性“StaffCode
”不匹配。
属性名称区分大小写。