首页 > 解决方案 > 尝试在 asp.net 框架 api 中执行 GetAllPatients 方法时出现 Stackoverflow 异常

问题描述

我正在构建一个 api 来从 mongo db 获取患者详细信息。但它正在抛出“堆栈溢出异常”。我尝试调试代码,但在方法完成后出现该方法时我没有得到任何异常。

控制器

 public class PatientController : ApiController
{
    readonly MongoContext _dbContext;
    public PatientController()
    {
        _dbContext = new MongoContext();

    }
    [Route("GetAllPatient")]
    public IHttpActionResult GetAllPatient()
    {
        var documents = _dbContext._database.GetCollection("Patient_Details");
        var document = documents.FindAll();
        return Content(HttpStatusCode.OK, documents);

    }

**模型 **

 public class Patient
{
    [BsonId]
    public ObjectId Id { get; private set; }

    [BsonElement("Name")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Name { set; get; }

    [BsonElement("Patient_Id")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_ID { set; get; }

    [BsonElement("Patient_Age")]
    [BsonRepresentation(BsonType.Int32)]
    public int Patient_Age { set; get; }

    [BsonElement("Patient_Gender")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Gender { set; get; }

    [BsonElement("Patient_Aadhar")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Aadhar { set; get; }

    [BsonElement("Patient_Email_ID")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Email_ID { set; get; }

    [BsonElement("Patient_Phone_No")]
    [BsonRepresentation(BsonType.Int64)]
    public int Patient_Phone_No { set; get; }

    [BsonElement("Patient_Address")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Address { set; get; }

    [BsonElement("Patient_Old_Clinical_MRI_Records")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Old_Clinical_MRI_Records { set; get; }

    [BsonElement("Patient_Old_Clinical_Diabetes_Records")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Old_Clinical_Diabetes_Records { set; get; }

    [BsonElement("Patient_Clinical_Records_for_Mother")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Clinical_Records_for_Mother { set; get; }

    [BsonElement("Patient_Clinical_Records_for_Father")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Clinical_Records_for_Father { set; get; }

    [BsonElement("Patient_Clinical_Records_for_Blood_Pressure")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Clinical_Records_for_Blood_Pressure { set; get; }

    [BsonElement("Patient_Old_Clinical_ECG_Records")]
    [BsonRepresentation(BsonType.String)]
    public string Patient_Old_Clinical_ECG_Records { set; get; }

    [BsonElement("Service_Code")]
    [BsonRepresentation(BsonType.String)]
    public string Service_Code { set; get; }

    [BsonElement("Services_Group")]
    [BsonRepresentation(BsonType.String)]
    public string Services_Group { set; get; }

    [BsonElement("Disease_Code")]
    [BsonRepresentation(BsonType.String)]
    public string Disease_Code { set; get; }

    [BsonElement("State")]
    [BsonRepresentation(BsonType.String)]
    public string State { set; get; }

    [BsonElement("Area")]
    [BsonRepresentation(BsonType.String)]
    public string Area { set; get; }

    [BsonElement("Branch_Code")]
    [BsonRepresentation(BsonType.String)]
    public string Branch_Code { set; get; }

    [BsonElement("Physician_Name")]
    [BsonRepresentation(BsonType.String)]
    public string Physician_Name { set; get; }

    [BsonElement("Consent_form_To_Hospital")]
    [BsonRepresentation(BsonType.String)]
    public string Consent_form_To_Hospital { set; get; }

    [BsonElement("Consent_Form_To_Relatives")]
    [BsonRepresentation(BsonType.String)]
    public string Consent_Form_To_Relatives { set; get; }

    [BsonElement("Consent_Form_To_Corp")]
    [BsonRepresentation(BsonType.String)]
    public string Consent_Form_To_Corp { set; get; }
}

Webapi配置

 public static void Register(HttpConfiguration config)
    {

        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );



    }

我没有得到实际的错误在哪里?任何帮助将不胜感激。

标签: asp.netmongodbapiasp.net-web-api

解决方案


推荐阅读