asp.net core 3 axios未能按预期发布

时间:2019-09-27 09:49:10

标签: javascript c# asp.net-core axios asp.net-core-3.0

在Web应用程序客户端,我使用axios发布请求。

这是我的axios帖子请求:

                axios({
                    method: 'post',
                    url: '/CorporateRequests/PostRequestProcess',
                    headers: { 'Content-Type': 'application/json' },
                    data: JSON.stringify(this.postingModel)
                }).then(function (response) {
                    if (response.status === 200 && response.data.status === 200) {
                        window.location.href = "/Service/Process";
                    } else {
                        self.$parent.showToast('HATA', 'Kayıt işlemi yapılamadı', 'error');
                    }
                }).catch(function (error) {
                    self.$parent.showToast('HATA', 'Kayıt işlemi yapılamadı', 'error');
                });

和处理来自客户端的发布请求的网址是这样的:

Task<JsonResult> PostRequestProcess([FromBody]RequestProcessModel postingModel)

但是以这种方式,在控制器端,我得到的是null postingModel,但是如果我更改了 RequestProcessModel->对象,我可以成功地从客户端获取以下值:

ValueKind = Object :
 "{"requestId":"026e1be4-f32e-42fe-b60b-4916bc572b0c",
 "editMode":false,
 "requestProcessId":null,
 "requestDetail":
 [
     {
        "processDetailId":null,
        "itemId":"df802578-83b5-4b0a-a4c2-08d72a38df96",
        "addBill":true,
        "description":"",
        "itemName":"Kod : KABLO - Adı : KABLO",
        "price":"234"
     },
     {
         "processDetailId":null,
         "itemId":"ca30ddba-be03-4b75-d7e2-08d738fd2efd",
         "addBill":true,
         "description":"",
         "itemName":"Kod : 200 - Adı : üst kapak",
         "price":"2343"
     }
]
         }"

这是我的 RequestProcessModel

public class RequestProcessModel
{
    public RequestProcessModel()
    {
        RequestDetail = new List<RequestProcessDetail>();
    }
    public Guid RequestId { get; set; }
    public bool EditMode { get; set; }
    public Guid? RequestProcessId { get; set; }
    public List<RequestProcessDetail> RequestDetail { get; set; }
}

public class RequestProcessDetail
{
    public Guid? ProcessDetailId { get; set; }
    public Guid ItemId { get; set; }
    public bool AddBill { get; set; }
    public string Description { get; set; }
    public string ItemName { get; set; }
    public decimal Price { get; set; }
}

我正在使用ASP.Net Core 3,请寻求帮助。

更新已解决

问题在于ASP.net core 3默认使用其自己的JSON API,但我认为它尚未准备好使用。 我向控制器添加了NewtonsoftJSON,并且效果很好。

services.AddControllers().AddNewtonsoftJson();

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您正在向控制器提交JSON字符串

data: JSON.stringify(this.postingModel)

您可以像这样更改代码

axios({
    method: 'post',
    url: '/CorporateRequests/PostRequestProcess',
    headers: { 'Content-Type': 'application/json' },
    data: this.postingModel
}).then(function (response) {
    if (response.status === 200 && response.data.status === 200) {
        window.location.href = "/Service/Process";
    } else {
        self.$parent.showToast('HATA', 'Kayıt işlemi yapılamadı', 'error');
    }
}).catch(function (error) {
    self.$parent.showToast('HATA', 'Kayıt işlemi yapılamadı', 'error');
});

或者您也可以使用此方法

axios.post(url, data, config)