没有MediaTypeFormatter可从asp.net mvc webapi中媒体类型为'text / html的内容中读取类型为'IEnumerable`1'的对象

问题描述 投票:0回答:1

在mvc中出现错误,没有MediaTypeFormatter可用于从媒体类型为'text / html的内容中读取类型为'IEnumerable`1'的对象

我正在一起使用Web API和MVC创建ASP.Net Web应用程序。我收到此错误:

没有MediaTypeFormatter可用于从媒体类型为'text / html的内容中读取类型为'IEnumerable`1'的对象

studenController(mvcproject)

namespace Mvcproj.Controllers
{
   public class studenController : Controller
   {
    public static HttpClient client = new HttpClient();

        // GET: Employes
        public ActionResult Index()
        {
            client.BaseAddress = new Uri("http://localhost:1747/api");
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            IEnumerable<studmodel> emplist;
            HttpResponseMessage response = client.GetAsync("studen").Result;
            Response.ContentType = "application/json";
            emplist = response.Content.ReadAsAsync<IEnumerable<studmodel>>().Result;          //here get an error
            response.EnsureSuccessStatusCode();
            return View(emplist);
        }
    }
}

Index.cshtml

@model IEnumerable<Mvcproj.Models.studmodel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.studid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.password)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.studid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.password)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

输出:

http://localhost:1785/studen/index

没有MediaTypeFormatter可用于从媒体类型为'text / html的内容中读取类型为'IEnumerable`1'的对象

studenController(api项目)

namespace WebsApi.Controllers
{
    public class studenController : ApiController
    {
        private studangEntities db = new studangEntities();

        // GET: api/studen
        public IQueryable<student> Getstudents()
        {
            return db.students;
        }

        // GET: api/studen/5
        [ResponseType(typeof(student))]
        public IHttpActionResult Getstudent(int id)
        {
            student student = db.students.Find(id);
            if (student == null)
            {
                return NotFound();
            }

            return Ok(student);
        }
      }
}

输出:

http://localhost:1747/api/studen

<ArrayOfstudent xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebsApi.Models">
<student>
<password>admin@123</password>
<studid>1017</studid>
<username>admin</username>
</student>
<student>
<password>devis</password>
<studid>1025</studid>
<username>devis</username>
</student>

我也更改:application / xml给出相同的严重错误

当我调试时:

内容{System.Net.Http.StreamContent}

状态码System.Net.HttpStatusCode.NotFound

requestmessage {方法:GET,RequestUri:'http://localhost:1747/studen',版本:1.1,内容:,标题:{接受:text / json}}

我引用此文章:https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/media-formatters,但错误未解决

如何解决此错误?

c# asp.net-web-api
1个回答
0
投票

您可以在这里看到

requestmessage {Method: GET, RequestUri: 'http://localhost:1747/studen', Version: 1.1, Content: , Headers: { Accept: text/json }}

请求URI为http://localhost:1747/studen,但您需要http://localhost:1747/api/studen

在基本URI的末尾添加/

client.BaseAddress = new Uri("http://localhost:1747/api/");
© www.soinside.com 2019 - 2024. All rights reserved.