如何修复Web API应用程序中的错误请求错误

时间:2018-02-08 20:23:02

标签: asp.net-web-api

您好我在我的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);
}

和行动方法运作良好

[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; }
    }
}

和我由fiddler提供的输入

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

1 个答案:

答案 0 :(得分:-1)

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