Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何提出邮寄申请?_C#_Json_Swagger_Postman_Asp.net Core Webapi - Fatal编程技术网

C# 我如何提出邮寄申请?

C# 我如何提出邮寄申请?,c#,json,swagger,postman,asp.net-core-webapi,C#,Json,Swagger,Postman,Asp.net Core Webapi,当我尝试提出POST请求时,如何解决我遇到的错误 我已尝试通过swagger执行以下json代码: { "productID": 0, "productName": "string", "price": 0, "marketID": 0, "market": { "marketID": 0, "name": "string", "budget": 0, "startDate": "2019-12-10", "productGuideID

当我尝试提出POST请求时,如何解决我遇到的错误

我已尝试通过swagger执行以下json代码:

{


"productID": 0,
  "productName": "string",
  "price": 0,
  "marketID": 0,
  "market": {
    "marketID": 0,
    "name": "string",
    "budget": 0,
    "startDate": "2019-12-10",
    "productGuideID": 0,
    "rowVersion": "string",
    "administrator": {
      "hireDate": "2019-12-10",
      "productAssignments": [
        null
      ],
      "countryAssignment": {
        "productGuideID": 0,
        "location": "string"
      },
      "id": 0,
      "lastName": "string",
      "firstMidName": "string"
    },
    "products": [
      null
    ]
  },
  "subscriptions": [
    {
      "subscriptionID": 0,
      "productID": 0,
      "customerID": 0,
      "customerLoyalty": 0,
      "customer": {
        "subscriptionDate": "2019-12-10",
        "subscriptions": [
          null
        ],
        "id": 0,
        "lastName": "string",
        "firstMidName": "string"
      }
    }
  ],
  "productAssignments": [
    {
      "productGuideID": 0,
      "productID": 0,
      "productGuide": {
        "hireDate": "2019-12-10",
        "productAssignments": [
          null
        ],
        "countryAssignment": {
          "productGuideID": 0,
          "location": "string"
        },
        "id": 0,
        "lastName": "string",
        "firstMidName": "string"
      }
    }
  ]
}
执行后,我想我从服务器得到了400响应: 我还认为我得到了200个成功回复: 我还尝试将来自swagger的标准json代码放入Postman,结果发现404错误:

以下是我的ProductsController中的Http Post方法:

[HttpPost]
    public async Task<ActionResult<Product>> PostProduct(Product product)
    {
        _context.Products.Add(product);
        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
            if (ProductExists(product.ProductID))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtAction("GetProduct", new { id = product.ProductID }, 

product);
        }
[HttpPost]
公共异步任务

我的产品型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace VitekSky.Models
{
public class Product
{

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Display(Name = "Product Number")]
    public int ProductID { get; set; }
    [StringLength(50, MinimumLength = 3)]
    public string ProductName { get; set; }


    [Range(0, 99999)]
    public int Price { get; set; }

    public int MarketID { get; set; }


    public Market Market { get; set; }    
    public ICollection<Subscription> Subscriptions { get; set; }
    public ICollection<ProductAssignment> ProductAssignments { get; set; }

}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
命名空间VitekSky.Models
{
公共类产品
{
[数据库生成(DatabaseGeneratedOption.None)]
[显示(名称=“产品编号”)]
public int ProductID{get;set;}
[StringLength(50,最小长度=3)]
公共字符串ProductName{get;set;}
[范围(099999)]
公共整数价格{get;set;}
公共ID{get;set;}
公开市场{get;set;}
公共ICollection订阅{get;set;}
公共ICollection ProductAssignments{get;set;}
}
}
我的市场模式:

 public class Market
{
    public int MarketID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    [DataType(DataType.Currency)]
    [Column(TypeName = "money")]
    public decimal Budget { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Start Date")]
    public DateTime StartDate { get; set; }

    public int? ProductGuideID { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }

    public ProductGuide Administrator { get; set; }
    public ICollection<Product> Products { get; set; }
}
公共类市场
{
公共ID{get;set;}
[StringLength(50,最小长度=3)]
公共字符串名称{get;set;}
[数据类型(数据类型.货币)]
[列(TypeName=“money”)]
公共十进制预算{get;set;}
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”,ApplyFormatInEditMode=true)]
[显示(Name=“开始日期”)]
公共日期时间起始日期{get;set;}
公共int?ProductGuideID{get;set;}
[时间戳]
公共字节[]行版本{get;set;}
public ProductGuide管理员{get;set;}
公共ICollection产品{get;set;}
}

当您将
“字符串”
绑定到
字节[]
行版本
为8字节数组时,会发生错误。每个字节代表一个64位整数的一部分,将为0-255

您不需要为
RowVersion
设置值,如果要创建模型,只需将其从json中删除即可

此外,您最好不要在不做任何更改的情况下将swagger的示例值复制为您的请求正文。它只提供了一个示例格式

在这种情况下,需要将
[null]
替换为null

工作的json可能类似于:

{
"productID": 0,
"productName": "string",
"price": 0,
"marketID": 0,
"market": {
  "marketID": 0,
  "name": "string",
  "budget": 0,
  "startDate": "2019-12-10",
  "productGuideID": 0,
  "administrator": {
    "hireDate": "2019-12-10",
    "productAssignments": null,
    "countryAssignment": {
      "productGuideID": 0,
      "location": "string"
    },
    "id": 0,
    "lastName": "string",
    "firstMidName": "string"
  },

  "products": null

},
"subscriptions": [
  {
    "subscriptionID": 0,
    "productID": 0,
    "customerID": 0,
    "customerLoyalty": 0,
    "customer": {
      "subscriptionDate": "2019-12-10",
      "subscriptions": null,

      "id": 0,
      "lastName": "string",
      "firstMidName": "string"
    }
  }
],
"productAssignments": [
  {
    "productGuideID": 0,
    "productID": 0,
    "productGuide": {
      "hireDate": "2019-12-10",
      "productAssignments": null,
      "countryAssignment": {
        "productGuideID": 0,
        "location": "string"
      },
      "id": 0,
      "lastName": "string",
      "firstMidName": "string"
    }
  }
]
}

我已经在ProductsController.cs类中查看了您的代码

我看到在你的招摇过市和邮递员中没有使用带有“Products”和“Prod”URI的post请求

/空气污染指数/产品及 /原料药/产品

这就是您面临404错误的原因


注意:如果您可以提供任何url,我可以尝试对其进行测试

您发送的负载看起来像是由swagger根据其知道的数据类型生成的示例。请看一看您的错误,它表示无法将其转换为字节数组。您的第二个映像没有告诉您使用200响应代码执行REST端点,这是一个200的示例。@gunr2171好的,因此错误与“错误”有关:{“$.market.rowVersion”:[“无法将JSON值转换为System.Byte[]。路径:$.market.rowVersion |行号:0 | BytePositionLine:176。“因此,使用名为rowVersions的字节数组似乎有问题。我使用它来解决数据库中的并发冲突。不确定如何解决无法转换为字节数组的问题。可能swagger的标准JSON不正确?因为在JSON中,它表示“rowVersion”:”字符串,Swagger提供了一个示例json负载,其中“rowVersion”属性设置为
string
,因为该属性是代码中的字符串。Swagger不够聪明,不知道稍后会将其转换为字节数组。您的模式实际上是什么样子?@gunr217 aa好的,谢谢。我在帖子中添加了我的产品模型和市场模型。我的rowVersion属性在我的市场c中姑娘。