ASP。NET MVC4 Web API应用程序定义post方法来保存客户。
客户在POST请求体中以json格式传递。
post方法中的客户参数包含属性的空值。
如何解决这个问题,以便张贴的数据将作为客户对象传递?
如果可能的话,Content-Type: application/x-www-form-urlencoded应该使用,因为我不知道如何在javascript方法中更改它。
控制器:
public class CustomersController : ApiController {
public object Post([FromBody] Customer customer)
{
return Request.CreateResponse(HttpStatusCode.OK,
new
{
customer = customer
});
}
}
}
public class Customer
{
public string company_name { get; set; }
public string contact_name { get; set; }
}
要求:
POST http://localhost:52216/api/customers HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
{"contact_name":"sdfsd","company_name":"ssssd"}
@model MVCClient.Models.ProductDetails
@{
ViewBag.Title = "ProductDetails";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Save").click(function () {
var ProductDetails = new Object();
ProductDetails.ProductName = $("#txt_productName").val();
ProductDetails.ProductDetail = $("#txt_desc").val();
ProductDetails.Price= $("#txt_price").val();
$.ajax({
url: "http://localhost:24481/api/Product/addProduct",
type: "Post",
dataType:'JSON',
data:ProductDetails,
success: function (data) {
alert('Updated Successfully');
//window.location.href = "../Index";
},
error: function (msg) { alert(msg); }
});
});
});
</script>
<h2>ProductDetails</h2>
<form id="form1" method="post">
<fieldset>
<legend>ProductDetails</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
<input id="txt_productName" type="text" name="fname">
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductDetail)
</div>
<div class="editor-field">
<input id="txt_desc" type="text" name="fname">
@Html.ValidationMessageFor(model => model.ProductDetail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
<input id="txt_price" type="text" name="fname">
@Html.ValidationMessageFor(model => model.Price)
</div>
<p>
<input id="Save" type="button" value="Create" />
</p>
</fieldset>
</form>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</form>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
使用JSON.stringify()来获取JSON格式的字符串,确保在进行AJAX调用时传递以下提到的属性:
contentType:“application / json”
下面是jquery代码让ajax post调用asp.net web api:
Var积=
JSON.stringify ({
productGroup:“Fablet”,
productId: 1、
productName:“Lumia 1525 64gb”,
sellingPrice: 700
});
. ajax({美元
URL:“http://localhost/api/Products”,
类型:“文章”,
contentType: application / json,
数据:产品,
成功:函数(数据,状态,xhr) {
alert('成功! ');
},
错误:函数(xhr,状态,错误){
alert('更新错误发生- ' +错误);
}
});
我一直在研究这个问题,发现了一个相当奇怪的结果。假设在c#中你的类中有这样的公共属性:
public class Customer
{
public string contact_name;
public string company_name;
}
那么你必须处理JSON。按照Shyju的建议,stringify trick,并如下所示:
var customer = {contact_name :"Scott",company_name:"HP"};
$.ajax({
type: "POST",
data :JSON.stringify(customer),
url: "api/Customer",
contentType: "application/json"
});
然而,如果你在你的类上定义getter和setter,就像这样:
public class Customer
{
public string contact_name { get; set; }
public string company_name { get; set; }
}
然后你可以更简单地称呼它:
$.ajax({
type: "POST",
data :customer,
url: "api/Customer"
});
这使用了HTTP报头:
Content-Type:application/x-www-form-urlencoded
我不太确定这里发生了什么,但它看起来像框架中的一个bug(功能?)假设不同的绑定方法调用不同的“适配器”,而application/json的适配器可以使用公共属性,而表单编码数据的适配器则不行。
不过,我不知道哪种做法才是最佳做法。