Stack Overflow error before entering Controller

1

I'm rewriting some old code from when I was at university in .NET Core's MVC, but some Actions simply don't work anymore. I'm getting StackOverflowException on places it never happened before. I copied the exception's details and they are like this:

System.StackOverflowException
  HResult=0x800703E9
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

I'll share my code to show what is going on... Here's one of the Views where I'm getting this problem:

@model TbCursos
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
@{
    ViewData["Title"] = "VisualizarCurso";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <section class="caixas">
        <input type="hidden" asp-for="@Model.Id"/>
        <div>
            <div class="div-nome-class">
                <label>@Model.Nome</label>
            </div>
            <br />
            @if (Model.CaminhoVideo != null)
            {
                <div>
                    <video src="~/user_files/videos/courses/@Model.CaminhoVideo" alt="@Model.Nome" controls class="visualizarcurso-video" />
                </div>
            }
            <div class="div-level">
                <label for="txtNivelCursos">Nivel</label>
                <label><span class="circlelevel">@Model.Nivel </span></label>
            </div>
            <div class="div-description">
                <label for="txtDescricaoCursos" style="font-size: 25px;">Descrição</label>
                <label class="description-class">@Model.Descricao</label>
            </div>

            @if (HttpContextAccessor.HttpContext.Request.Cookies["UserId"] != null)
            {
                if (Convert.ToInt32(HttpContextAccessor.HttpContext.Request.Cookies["UserId"]) != Model.IdUsuarioCriador)
                {
                    <div class="div-buttoningressar">
                        <a asp-action="IngressarCursos" asp-controller="Cursos" asp-route-IdCurso="@Model.Id">Ingressar</a>
                    </div>
                }
                else
                {
                    <div id="popup1" class="overlay-login">
                        <div class="popup1">
                            <h2>Você criou este curso, então não poderá ingressá-lo!</h2>
                            <a class="close1" href="#popup1">&times;</a>
                            <div class="content1">
                                <a href="/Cursos/" class="pop-link" style="padding: 0.5em 1.0em;">Voltar</a>
                                <br />
                            </div>
                        </div>
                    </div>
                }
            }
            else
            {
                <div id="popup1" class="overlay-login">
                    <div class="popup1">
                        <h2>Faça o login ou cadastre-se para ter acesso a esse curso!</h2>
                        <a class="close1" href="#popup1">&times;</a>
                        <div class="content1">
                            <a href="/Login/Login" class="pop-link" style="padding: 0.5em 1.0em;">Cadastre-se / Faça login</a>
                            <br />
                        </div>
                    </div>
                </div>
            }
        </div>
    </section>
</div>
<br />
<br />

Here is this View's Action code:

    public IActionResult VisualizarCurso(TbCursos model, int IdCurso)
    {
         try
         {
             TbCursos Cursos = new CursosRepository().Consultar(IdCurso);
             return View(Cursos);
         }
         catch (Exception e)
         {
             Log.GravarLogErro(1, "Erro ao Visualizar Curso", Path.GetFileName(Request.GetDisplayUrl()), e);
             return View("~/Views/Shared/Error.cshtml");
         }
    }

I suppose the error is related to the model binding itself mostly because of that if I remove the model from the View, everything works fine. The exception also occurs before executing the Action's code. I'll also share the EF Core generated model "TbCursos":

using System.Collections.Generic;

namespace Ark.Entidades.Models
{
     public partial class TbCursos
     {
          public TbCursos()
          {
               InverseIdCursoPaiNavigation = new HashSet<TbCursos>();
               TbAulas = new HashSet<TbAulas>();
               TbUsuarioCursos = new HashSet<TbUsuarioCursos>();
          }

          public int Id { get; set; }
          public int? IdCursoPai { get; set; }
          public int IdUsuarioCriador { get; set; }
          public int IdCategoria { get; set; }
          public int QuantidadeAulas { get; set; }
          public string Nome { get; set; }
          public string Descricao { get; set; }
          public int Nivel { get; set; }
          public DateTime? DataCriacao { get; set; }
          public string CaminhoVideo { get; set; }
          public string CaminhoThumb { get; set; }
          public bool? Ativo { get; set; }

          public virtual TbCategorias IdCategoriaNavigation { get; set; }
          public virtual TbCursos IdCursoPaiNavigation { get; set; }
          public virtual TbUsuarios IdUsuarioCriadorNavigation { get; set; }
          public virtual ICollection<TbCursos> InverseIdCursoPaiNavigation { get; set; }
          public virtual ICollection<TbAulas> TbAulas { get; set; }
          public virtual ICollection<TbUsuarioCursos> TbUsuarioCursos { get; set; }
     }
}

And here are some other partial classes I made to extend that class:

using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations.Schema;

namespace Ark.Entidades.Models
{
     public partial class TbCursos
     {
          [NotMapped]
          public IFormFile Arquivo { get; set; }

          [NotMapped]
          public IFormFile ArquivoVideo { get; set; }
     }
}

And this one:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace Ark.Entidades.Models
{
     public partial class TbCursos
     {
          [NotMapped]
          public List<TbAulas> Aulas { get; set; }

          [NotMapped]
          public List<TbAulas> AulasLiberadas { get; set; }

          [NotMapped]
          public List<TbAulas> AulasNaoLiberadas { get; set; }
     }
}

The code itself isn't that clean, but I'll work on it after making everything at least run. Thanks for your attention!

c#
asp.net-core
asp.net-core-mvc
stack-overflow
ef-core-2.2
asked on Stack Overflow Aug 2, 2019 by MilitanteDoPT • edited Aug 2, 2019 by MilitanteDoPT

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0