首页 > 解决方案 > mscorlib.dll 中出现“System.AggregateException”类型的异常,但未在用户代码中处理

问题描述

您好,我正在尝试使用 rest web 服务并在 chtml 中公开它,但是当我启动我的应用程序时我不断收到这个错误,我不明白它。是因为缺少库或依赖项吗?

缺勤控制器:

using ProjectWeb.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Mvc;
namespace ProjectWeb.Controllers
{
    public class AbsenceController : Controller
    {
        // GET: Absence
        public ActionResult Index()
        {
            HttpClient Client = new System.Net.Http.HttpClient();
            Client.BaseAddress = new Uri("http://localhost:18080/PiDev-web/rest/absence/");
            Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = Client.GetAsync("all").Result;
            if (response.IsSuccessStatusCode)
            {
                ViewBag.result = response.Content.ReadAsAsync<IEnumerable<Absence>>().Result;
                ///ViewBag.result1 = response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                ViewBag.result = "error";
            }

            return View("index");
        }

        // GET: Absence/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Absence/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Absence/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Absence/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Absence/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Absence/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Absence/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

我的 index.chtml:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>index</title>
</head>
<body>
    <br />
    <h3>
        @Html.ActionLink("Ajouter Absence ", "Create")
    </h3>
    <hr />
    <table class="table">
        <tr>
            <th>ID</th>
            <th>Nom</th>

            <th>Action</th>
        </tr>

        @foreach (var h in ViewBag.result)
        {
            <tr>
                <td>@h.idAbsence</td>
                <td>@h.cause</td>



            <td>
                @Html.ActionLink("Edit", "Edit", new { id = h.idAbsence }) |
                @Html.ActionLink("Details", "Details", new { id = h.idAbsence }) |
                @Html.ActionLink("Delete", "Delete", new { id = h.idAbsence })
            </td>
        </tr>
        }


    </table>
</body>
</html>

当我通过按浏览器上的视图启动它时,我收到此错误 ,请在此处输入图像描述

当我按下它时,错误是由以下原因引起的:

 ViewBag.result =response.Content.ReadAsAsync<IEnumerable<Absence().Result;

如果您熟悉此错误,请帮助我

标签: asp.net.netasp.net-mvcrestexception

解决方案


在您的情况下, AggregateException 可能意味着任务引发了异常。您可以在调试选项下打开“Break On All Exceptions”,然后再次运行以找出原始异常。

哦,取消选择“仅启用我的代码”


推荐阅读