【问题标题】:How to fix bad request error in the web API application如何修复 Web API 应用程序中的错误请求错误
【发布时间】:2018-02-14 03:02:20
【问题描述】:

您好,我在我的 post 方法中收到错误请求错误消息,而我的项目的另一个 post 方法具有相同类型的运行良好的代码。下面我将编写这两个 Action 方法

给出错误请求错误消息的方法

[HttpPost]
[Route("api/tblProducts/AddToCart", Name = "GetAddWishList")]
public IHttpActionResult AddToCartList(tblWishList wishList)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var AltAddress = db.AddToCart(wishList.PrdName, wishList.PrdPrice, wishList.DisPrice, wishList.PrdStar, wishList.PrdDetails, wishList.UserId, wishList.Extra3, wishList.Extra4, wishList.PrdOffPercentage, wishList.ProductId);

    return CreatedAtRoute("GetAddWishList", new { id = wishList.UserId }, wishList);
}

以及运行良好的Action方法

[Route("api/tblProducts/DynamicImage", Name ="GetProductImage")]
public IHttpActionResult AddDynamic(tblImage Review)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var AltAddress = db.InsertDynamicImage(Review.ImageId, Review.ImagePath, Review.ImageCategory);

    return CreatedAtRoute("GetProductImage", new { id = Review.ImageId }, Review);
}

更新: 我的 tblWishlist 类代码

namespace MyMakeAnOrder.com.Models
{
    using System;
    using System.Collections.Generic;

    public partial class tblWishList
    {


        public string PrdName { get; set; }
        public decimal? PrdPrice { get; set; }
        public decimal? DisPrice { get; set; }
        public string PrdStar { get; set; }
        public string PrdDetails { get; set; }
        public string UserId { get; set; }
        public string WishStatus { get; set; }
        public string CartStatus { get; set; }
        public string Extra3 { get; set; }
        public string Extra4 { get; set; }
        public byte? PrdOffPercentage { get; set; }
        public string ProductId { get; set; }

        public virtual tblUser tblUser { get; set; }
    }
}

以及我由提琴手提供的输入

{"PrdName":"Nikeshoe","PrdPrice":"3000","DisPrice":"2000","PrdStar","PrdDetails":"Product is very good","UserId":"User1002","Extra3":"nothing","Extra4":"nothing","PrdOffPercentage":"20","ProductId":"Prd101"}

【问题讨论】:

  • 你调试了吗?你得到模型验证了吗
  • 是的,我对其进行了调试,问题是在模型状态下,如果 block 它给出 false 并且在 tblWishList 对象中给出 null 意味着我传递的内容没有在操作方法中达到
  • 所以问题不同,请确保传递正确的输入或修改您的问题
  • 好的,我正在添加 tblWishList 类代码和我在问题中的输入
  • 您的输入不是有效的 json

标签: asp.net-web-api


【解决方案1】:

您收到 BadRequest 是因为 ModelState 无效。在方法开始处设置断点并检查 ModelState 包含的内容。您将收到有关验证错误的信息。

【讨论】:

  • 是的,我对其进行了调试,问题是在模型状态下,如果 block 它给出 false 并且在 tblWishList ' 对象中给出 null 意味着我通过提琴手传递的数据在操作方法中没有达到,请帮帮我
  • 你能显示tblWishList类的定义和fiddler传递的数据吗?
  • 好的,我正在添加 tblWishList 类代码和我在问题中的输入
  • 这个错误信息也是fiddler给出的{"Message":"The request is invalid.","ModelState":{"wishList":["解析值时遇到意外字符: g.路径'',第 0 行,位置 0。"]}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多