我如何将json对象传递给webapi控制器(POST)并且没有将其映射到的类,而是将其作为任意内容处理?
所以如果我这样从我的客户端传入:
createRecord: function (model, data, callback, callbackParams) {
var request = jQuery.ajax({
type: "POST", // default = GET,
url: '/api/' + model + '/',
data: data,
contentType: 'application/json',
success: function (msg) {
$('#results').text(msg);
if (callback) // only fire a callback if it has been specified
callback(msg, callbackParams);
},
error: function (jqXHR, textStatus) {
alert('Request failed: ' + textStatus);
}
});
}
Run Code Online (Sandbox Code Playgroud)
和数据是这样的:
{ "_id" : ObjectId("5069f825cd4c1d590cddf206"), "firstName" : "John", "lastName" : "Smith", "city" : "Vancouver", "country" : …Run Code Online (Sandbox Code Playgroud) 传递参数时出错,
"无法绑定多个参数"
这是我的代码
[HttpPost]
public IHttpActionResult GenerateToken([FromBody]string userName, [FromBody]string password)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
阿贾克斯:
$.ajax({
cache: false,
url: 'http://localhost:14980/api/token/GenerateToken',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: { userName: "userName",password:"password" },
success: function (response) {
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(jqXhr.responseText);
alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText + " " + jqXhr.status);
},
complete: function (jqXhr) {
},
})
Run Code Online (Sandbox Code Playgroud) 如何使用Angular2正确创建复杂对象或多个参数的Web API POST?
我在Angular2中有一个服务组件,如下所示:
public signin(inputEmail: string, inputPassword: string): Observable<Response> {
return this.http.post('/api/account/signin', JSON.stringify({ Email: inputEmail, Password: inputPassword}), this.options);
}
Run Code Online (Sandbox Code Playgroud)
目标web api如下所示:
[HttpPost]
[Route("signin")]
public async Task<IActionResult> Signin(string email, string password)
{
....
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为我需要将web api的参数转换为具有Email和Password属性的单个POCO类实体,并放置[FromBody]属性: Signin([FromBody] Credential credential)
如果不使用[FromURI](带有查询字符串的POST请求?),如何在不将这些参数转换为单个POCO类的情况下对多个参数或复杂对象进行POST?
因为如果我有许多带有参数的Web API POST操作,(string sensitiveInfo1, string name, int sensitiveInfo2)或者(ClassifiedInfo info, string sensitiveInfo1, string sensitiveInfo2),我是否需要将它们全部转换为POCO类并始终使用[FromBody]?
PS.我以前使用RestangularJS它,它可以发布任何东西(多个原始对象和复杂对象),而我的Web API操作没有[FromBody]属性.将研究RestangularJS如何做到这一点.
asp.net asp.net-web-api restangular asp.net-web-api2 angular
我使用的是asp.net 4.5和最新的MVC版本.在html页面中发布表单时deviceId,我需要retrive 和bodyheader.在行public void Post([FromBody]string value)值处,它始终为null.
我在这做错了吗?
namespace WebApi.Controllers
{
public class NotificationController : ApiController
{
// GET api/notification
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/notification/5
public string Get(int id)
{
return "value";
}
// POST api/notification
public void Post([FromBody]string value)
{
// value it is always null
}
// PUT api/notification/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/notification/5
public void Delete(int …Run Code Online (Sandbox Code Playgroud) 我有一个带有更新操作的 ASP.Net Core WebApi,如下所示:
[HttpPut("{id}")]
public async Task<IActionResult> Campaigns(long id, JObject jobject)
{
}
Run Code Online (Sandbox Code Playgroud)
当我从邮递员点击这个端点时的请求和响应如下:-
{
"Zone_Code": 1,
"State_Code": 24,
"City_Code": 25,
"Sales_Associate": null,
"Operation_Associate": null,
"Traveller": null,
"Target_Sector": 5,
"Campaign_DateTime": "2020-04-04T00:00:00",
"Format": 2,
"Campaign_Name": "Test",
"Data_Criteria": null,
"IsActive":"Y",
"Stage": "Initiation"
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|58a198b0-4ac4ca0838cdebce.",
"errors": {
"$": [
"The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $ | LineNumber: 1 | BytePositionInLine: 8."
]
}}
Run Code Online (Sandbox Code Playgroud)