c# - 此资源不支持“multipart/form-data”

标签 c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

我有 WEbAPI2 后端。

我尝试从 postman 发送表单数据

enter image description here

但是得到这个错误

"No MediaTypeFormatter is available to read an object of type 'StartWorkingDay' from content with media type 'multipart/form-data'.",

这是我的 Controller 代码

// POST: api/StartWorkingDays
[ResponseType(typeof(StartWorkingDay))]
public IHttpActionResult PostStartWorkingDay(StartWorkingDay startWorkingDay)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    db.StartWorkingDays.Add(startWorkingDay);
    db.SaveChanges();

    return CreatedAtRoute("DefaultApi", new { id = startWorkingDay.Id }, startWorkingDay);
}

当我通过原始数据发送时,一切正常

enter image description here

如何让它接收multipart/form-data

最佳答案

取自MSDN

application/x-www-form-urlencoded

Form data is encoded as name/value pairs, similar to a URI query string. This is the default format for POST.

多部分/表单数据

Form data is encoded as a multipart MIME message. Use this format if you are uploading a file to the server.

在 postman 中使用 enctype x-www-form-urlencoded,而不是 form-data

关于c# - 此资源不支持“multipart/form-data”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072864/

相关文章:

c# - 本地化不适用于 ASPX 页面中的所有标记/标签

c# - Entity Framework 添加新实体

javascript - 一个像幻灯片一样工作的单页网站

C# 向 Minecraft 发送关键输入以移动玩家

.net - 加载 ASP.NET DropDownList 对象

c# - 显示无/删除样式背后的asp.net 代码不起作用

asp.net-mvc - 没有特定 Controller 的 ASP.NET MVC 3 路由

c# - ASP.Net MVC 3 登录和 Windows 身份验证

asp.net-mvc - 临时数据持久化

c# - 少量数据中的 NamedPipe 与 TCP/IP