因此,在我的角前端应用程序中,我有一个表单,在类型记录文件中,我创建了一个新的formdata对象,并将项目附加到其中,如下所示:
public postFormData() {
const form = $('#customListForm')[0];
const formData = new FormData();
formData.append('tenant', form[0].value);
formData.append('username', form[1].value);
formData.append('password', form[2].value);
formData.append('CL_title', form[3].value);
formData.append('CL_description', form[4].value);
formData.append('CL_contentType', form[5].value);
formData.append('CL_template', form[6].value);
this.http.post(this.apiURL, formData).catch(this.errorHandler).subscribe(res => this.formPosted(res));
// this.http.get(this.apiURL).catch(this.errorHandler).subscribe(res => this.formPosted(res));
} 最后,我的formData被正确地填写了。现在,当我试图将它发布到我的ASP.NET 4.6.1WebAPI时,它会给出以下错误:
POST http://localhost:52613/api/customlist 415 (Unsupported Media Type) 在这里您可以看到我的API代码:
// POST api/customlist
[HttpPost]
public System.Web.Http.IHttpActionResult Post(CustomList data)
{
var test = HttpContext.Current.Request;
var testje = data;
return Ok("POST all good");
} 我认为"CustomList数据“是错误的?我想要的是formData绑定到我的模型,我的模型有与您在这里看到的相同的字段:
public class CustomList
{
public string Tenant { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string CL_title { get; set; }
public string CL_description { get; set; }
public string CL_contentType { get; set; }
public string CL_template { get; set; }
} 在我的标题中,我可以看到以下内容:
Response Headers: Content-Type: application/json
Request Headers: Content-Type: multipart/form-data 这会是问题吗?
这里有一个奇怪的部分:我以前也创建了一个.NET Core2WebAPI,我的post操作看起来如下(忽略文件内容):
// POST api/input
[HttpPost]
public async Task<IActionResult> Post(SPInputObject data)
{
var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
string filePath = Path.Combine(uploads, data.File.FileName);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
if (data.File.Length > 0)
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await data.File.CopyToAsync(fileStream);
}
}
ProvisioningService service = new ProvisioningService();
service.CreateSiteCollection(data.Tenant, data.Title, filePath);
return Ok("POST all good");
} 当我将相同的角度形式/格式数据发布到这个api控制器+动作时,它将输入post .那么,为什么可以将它发送到我的.NET Core2WebAPI而不是我的ASP.NET 4.6.1WebAPI呢?
有人知道问题出在哪里吗?
编辑:尝试将内容类型添加到前端的角度请求不起作用,下面是我尝试过的:
private httpOptions = {
headers: new Headers({
'Content-Type': 'application/json'
})
}; 修改我的http帖子(我添加了this.httpOptions):
this.http.post(this.apiURL, formData,
this.httpOptions).catch(this.errorHandler).subscribe(res =>
this.formPosted(res)); 然后,它在我的this.httpOptions上给出了以下错误:
"message": "Argument of type '{ headers: Headers; }' is not assignable to parameter of type 'RequestOptionsArgs'.\n Types of property 'headers' are incompatible.\n Type 'Headers' is not assignable to type 'Headers'. Two different types with this name exist, but they are unrelated.\n Property 'keys' is missing in type 'Headers'."发布于 2018-04-11 15:22:38
你在发FormData。大多数浏览器在提供"multipart/form-data"实例时自动将Content设置为FormData。如果修改代码以使用Content "application/json",那么还应该修改请求对象,使其不使用FormData,而是使用JSON:
public postFormData() {
const form = $('#customListForm')[0];
const requestData = {
tenant: form[0].value,
username: form[1].value,
password: form[2].value,
CL_title: form[3].value,
CL_description: form[4].value,
CL_contentType, form[5].value,
CL_template: form[6].value
};
this.http.post(this.apiURL, requestData);
} 发布于 2018-04-11 13:42:27
尝试将内容类型设置为application/json,并在角侧或asp.net侧将编码设置为utf-8,这两者都更好。通常是这两件事造成的。Asp.net核心的模型绑定工作方式略有不同。这是一篇很好的文章
https://stackoverflow.com/questions/49776155
复制相似问题