c# - 没有 MediaTypeFormatter 可用于读取 asp.net web api 中类型为 'Advertisement' 的对象

标签 c# asp.net asp.net-web-api2 asp.net-identity

我有一个名为 Advertisement 的类:

 public class Advertisement
{
    public string Title { get; set; }
    public string Desc { get; set; }
}

在我的 Controller 中:

public class OrderController : ApiController
{
    public UserManager<IdentityUser> UserManager { get; private set; }

    // Post api/Order/Test

    [Route("Test")]
    public IHttpActionResult Test(Advertisement advertisement)
    {
        var currentUser = User.Identity.GetUserId();
        Task<IdentityUser> user = UserManager.FindByIdAsync(currentUser);

      return Ok(User.Identity.GetUserId());
    }

但是当我用 Postman 测试它时,我遇到了这个错误,

 "Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Advertisement' from content with media type 'application/octet-stream'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"

任何人都可以帮助我吗?

最佳答案

在您的 WebApiConfig.cs 中将其添加到寄存器中

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));

关于c# - 没有 MediaTypeFormatter 可用于读取 asp.net web api 中类型为 'Advertisement' 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544009/

相关文章:

javascript - div 值在 jQuery 中未定义

c# - 业务对象方法

oauth-2.0 - 连接 Auth token 并将其传递到启用了 CORS 的 SignalR 集线器

c# - API 工作正常但不接受参数 c#

c# - 如何在桌面上始终保持窗口窗体的四个角可见?

c# - 如何在 MailMessage 中正确添加 CSS

c# - 从方法外部获取到操作的路径

c# - 获取列表中具有当前 ID 的对象之后的下一个对象

asp.net - 在没有 DataBind 的情况下向 GridView 添加新行

ASP.NET 表单例份验证在 iPad 上不断显示登录页面