【问题标题】:No MediaTypeFormatter is available to read an object of type 'IEnumerable`1' from content with media type 'text/html'没有 MediaTypeFormatter 可用于从媒体类型为“text/html”的内容中读取类型为“IEnumerable`1”的对象
【发布时间】:2018-01-01 08:13:49
【问题描述】:

我创建了一个 web api 和一个 mvc 应用程序来使用该 api。在我的 mvc 员工控制器类中,我编写了使用 web api 的代码。但是当我运行应用程序时它会抛出一个异常,说明“System.Net.Http.Formatting.dll 中发生了“System.Net.Http.UnsupportedMediaTypeException”类型的异常,但未在用户代码中处理 附加信息: 没有 MediaTypeFormatter 可用于从媒体类型为“text/html”的内容中读取“IEnumerable`1”类型的对象。 下面是mvc应用控制器类代码。

public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {

                IEnumerable<mvcEmployeeModel> emplist;
                HttpResponseMessage response = GlobalVariables.WebApiClient.GetAsync("Employee").Result;
                emplist =response.Content.ReadAsAsync<IEnumerable<mvcEmployeeModel>>().Result;// gets exception here
                return View(emplist);



        }
    }
}

请帮助解决这个问题。

【问题讨论】:

    标签: c# .net rest


    【解决方案1】:

    将 Content-Type: 更改为 'application/json' 或 Content-Type:'application/xml'

    Content-Type:'application/json'
    

    Content-Type:'application/xml'
    

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2012-09-12
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多