[英]Trying to send FormData from Angular 5 app to ASP.NET 4.6.1 Web API : getting an “unsupported media type” error
So in my Angular front-end app I have a form and in the typescript file I create a new formdata object and append the items to it like so: 因此,在我的Angular前端应用程序中,我有一个表单,在打字稿文件中,我创建了一个新的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));
}
In the end, my formData is filled in correctly. 最后,我的formData已正确填写。 Now when I try to POST this to my ASP.NET 4.6.1 Web API, it gives me the following error:
现在,当我尝试将此发布到我的ASP.NET 4.6.1 Web API时,它给了我以下错误:
POST http://localhost:52613/api/customlist 415 (Unsupported Media Type)
Here you can see my API code: 在这里您可以看到我的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");
}
I assume the "CustomList data" is wrong? 我假设“ CustomList数据”是错误的? What I'd love is that the formData binds to my model, my model has the same fields as you can see here:
我想要的是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; }
}
In my headers I can see the following: 在标题中,我可以看到以下内容:
Response Headers: Content-Type: application/json
Request Headers: Content-Type: multipart/form-data
Could this be the issue? 这可能是问题吗?
Here's the weird part: I had previously created a .NET Core 2 Web API as well, and my post action looks like this (ignore the file stuff): 这是一个很奇怪的部分:我之前也创建了.NET Core 2 Web API,我的发布操作如下所示(忽略文件内容):
// 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");
}
When I post my SAME Angular form/formdata to this api controller + action, it will enter the POST .... So why is it ok sending it to my .NET Core 2 Web API but not my ASP.NET 4.6.1 Web API? 当我将SAME Angular表单/表单数据发布到此api控制器+操作时,它将输入POST...。那么为什么可以将其发送到.NET Core 2 Web API而不是ASP.NET 4.6.1 Web上呢? API?
Anyone know what could be the problem? 有人知道可能是什么问题吗?
EDIT: trying to add content-type to my front-end Angular request doesn't work, here's what I tried: 编辑:尝试将内容类型添加到我的前端Angular请求不起作用,这是我尝试的方法:
private httpOptions = {
headers: new Headers({
'Content-Type': 'application/json'
})
};
Modifying my http post (i've added this.httpOptions): 修改我的http帖子(我添加了this.httpOptions):
this.http.post(this.apiURL, formData,
this.httpOptions).catch(this.errorHandler).subscribe(res =>
this.formPosted(res));
It then gives the following error on my this.httpOptions: 然后在我的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'."
Try to setup the content type application/json and encoding to utf-8 on the angular side, or on the asp.net side, better on both. 尝试在角度侧或asp.net侧设置内容类型application / json和utf-8编码,两者都更好。 Usually it's caused by those two things.
通常是由这两件事引起的。 Asp.net core's model binding works slightly differently.
Asp.net核心的模型绑定工作略有不同。 Here's a good article about that
这是一篇很好的文章
Model binding JSON POSTs in ASP.NET Core ASP.NET Core中的模型绑定JSON POST
You're posting FormData
. 您正在发布
FormData
。 Most browsers automatically set the Content-Type to "multipart/form-data"
when you supply an instance of FormData
. 当您提供
FormData
的实例时,大多数浏览器会自动将Content-Type设置为"multipart/form-data"
。 If you modify the code to use Content-Type "application/json"
then you should also modify your request object to not use FormData
, but JSON: 如果您修改代码以使用Content-Type
"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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.