问题描述
我有一个在.net核心项目中运行的Angular 9应用程序。我可以将单个文件和其他表单详细信息传递给该项目,
public async Task<CRMNote> AddFileToOpportunity(IFormFile file,[FromForm]CRMNoteViewModel model)
{
var inputModel = new CRMNote()
{
DateTime = DateTime.Now,Note = model.NoteContents,OpportunityId = Int32.Parse(model.OpportunityId),User = model.Username,Attachment = file
};
return await ApiCaller.CallPostWebApi(inputModel,"urlToCall");
和班级
public class CRMNote
{
public int Id { get; set; }
public int OpportunityId { get; set; }
public string Note { get; set; }
public string User { get; set; }
public DateTime DateTime { get; set; }
public IFormFile Attachment { get; set; }
}
但是当我尝试调用后端api时,我收到一个错误的请求,因为我试图将其作为application / json而不是multipart / form-data传递。
这是我的API调用者:
public static async Task<T> CallPostWebApi<T>(T t,string serviceUrl)
{
using var client = new HttpClient { BaseAddress = new Uri(serviceUrl) };
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var json = JsonConvert.SerializeObject(t);
var formData = new StringContent(json,Encoding.UTF8,"application/json");
var response = await client.PostAsync(serviceUrl,formData);
有没有办法在一次调用中传递json数据和表单?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)