首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试将FormData从角5应用程序发送到ASP.NET 4.6.1WebAPI:获取“不支持的媒体类型”错误

尝试将FormData从角5应用程序发送到ASP.NET 4.6.1WebAPI:获取“不支持的媒体类型”错误
EN

Stack Overflow用户
提问于 2018-04-11 13:16:08
回答 2查看 4K关注 0票数 2

因此,在我的角前端应用程序中,我有一个表单,在类型记录文件中,我创建了一个新的formdata对象,并将项目附加到其中,如下所示:

代码语言:javascript
复制
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时,它会给出以下错误:

代码语言:javascript
复制
POST http://localhost:52613/api/customlist 415 (Unsupported Media Type) 

在这里您可以看到我的API代码:

代码语言:javascript
复制
// 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绑定到我的模型,我的模型有与您在这里看到的相同的字段:

代码语言:javascript
复制
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; }
}  

在我的标题中,我可以看到以下内容:

代码语言:javascript
复制
Response Headers: Content-Type: application/json
Request Headers: Content-Type: multipart/form-data  

这会是问题吗?

这里有一个奇怪的部分:我以前也创建了一个.NET Core2WebAPI,我的post操作看起来如下(忽略文件内容):

代码语言:javascript
复制
// 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呢?

有人知道问题出在哪里吗?

编辑:尝试将内容类型添加到前端的角度请求不起作用,下面是我尝试过的:

代码语言:javascript
复制
private httpOptions = {
  headers: new Headers({
    'Content-Type': 'application/json'
  })
};  

修改我的http帖子(我添加了this.httpOptions):

代码语言:javascript
复制
this.http.post(this.apiURL, formData, 
this.httpOptions).catch(this.errorHandler).subscribe(res => 
this.formPosted(res));  

然后,它在我的this.httpOptions上给出了以下错误:

代码语言:javascript
复制
 "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'."
EN

回答 2

Stack Overflow用户

发布于 2018-04-11 15:22:38

你在发FormData。大多数浏览器在提供"multipart/form-data"实例时自动将Content设置为FormData。如果修改代码以使用Content "application/json",那么还应该修改请求对象,使其不使用FormData,而是使用JSON:

代码语言:javascript
复制
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);
}  
票数 1
EN

Stack Overflow用户

发布于 2018-04-11 13:42:27

尝试将内容类型设置为application/json,并在角侧或asp.net侧将编码设置为utf-8,这两者都更好。通常是这两件事造成的。Asp.net核心的模型绑定工作方式略有不同。这是一篇很好的文章

ASP.NET内核中的模型绑定JSON帖子

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49776155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档