Asp.net web api 在dotnet core web API中接收多部分表单数据
我正在开发一个web API,它需要在模型中接收由多个部分组成的表单数据。到目前为止,当我从邮递员那里尝试时,它没有收到请求并显示错误的请求。 我的模型:Asp.net web api 在dotnet core web API中接收多部分表单数据,asp.net-web-api,asp.net-core-webapi,webapi,Asp.net Web Api,Asp.net Core Webapi,Webapi,我正在开发一个web API,它需要在模型中接收由多个部分组成的表单数据。到目前为止,当我从邮递员那里尝试时,它没有收到请求并显示错误的请求。 我的模型: public class InvoiceDetails { public int? po_id { get; set; } public DateTime created_date { get; set; } public string grn_status { get; set
public class InvoiceDetails
{
public int? po_id { get; set; }
public DateTime created_date { get; set; }
public string grn_status { get; set; }
public int utilization_amount { get; set; }
public int currency_id { get; set; }
public string currency_code { get; set; }
public string currency_symbol { get; set; }
[FromForm(Name = "invoice_file")]
public List<IFormCollection> invoice_file { get;set;}
[FromForm(Name = "other_files")]
public List<IFormCollection> other_files { get; set; }
}
如何制作动作方法或模型,以便用户可以将包含多个文件的模型上载到属性“其他文件”和“发票文件”
参考邮递员图像如
CodeCaster
所述,添加内容类型:多部分/表单数据
并将List
更改为List
。这并不是将整个模型更改为List。因此,您还可以使用idobj.xxx.change检索模型中存在的其他信息
public class InvoiceDetails
{
public int? po_id { get; set; }
public DateTime created_date { get; set; }
public string grn_status { get; set; }
public int utilization_amount { get; set; }
public int currency_id { get; set; }
public string currency_code { get; set; }
public string currency_symbol { get; set; }
[FromForm(Name = "invoice_file")]
public List<IFormCollection> invoice_file { get;set;}
[FromForm(Name = "other_files")]
public List<IFormCollection> other_files { get; set; }
}
而且您需要获取要用于IFormCollection的每个键的数据,因此
公共列表发票文件{get;set;}
优于使用IFormCollection。您的意思是列表
?另外,显示您的邮递员请求。@CodeCaster,是的,但它应该接收多个文件。请检查我的参考PostMan图像。@CodeCaster在您提供的链接中,他只能发布文件(一个参数)。但在这里,我期待的对象可能也包含文件。如何以这种方式设计API或操作方法..使用List
并设置适当的请求头。不要因为存在差异而忽略相关链接,请查看相似之处,在本例中为错误消息。谢谢。成功了。
public class InvoiceDetails
{
public int? po_id { get; set; }
public DateTime created_date { get; set; }
public string grn_status { get; set; }
public int utilization_amount { get; set; }
public int currency_id { get; set; }
public string currency_code { get; set; }
public string currency_symbol { get; set; }
[FromForm(Name = "invoice_file")]
public List<IFormCollection> invoice_file { get;set;}
[FromForm(Name = "other_files")]
public List<IFormCollection> other_files { get; set; }
}
public class InvoiceDetails
{
public int? po_id { get; set; }
public DateTime created_date { get; set; }
public string grn_status { get; set; }
public int utilization_amount { get; set; }
public int currency_id { get; set; }
public string currency_code { get; set; }
public string currency_symbol { get; set; }
[FromForm(Name = "invoice_file")]
public List<IFormFile> invoice_file { get; set; }
[FromForm(Name = "other_files")]
public List<IFormFile> other_files { get; set; }
}
public IActionResult CreateInvoice([FromForm]IFormCollection idobj)