Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# I';我在get方法中遇到堆栈溢出异常_C#_Asp.net Mvc - Fatal编程技术网

C# I';我在get方法中遇到堆栈溢出异常

C# I';我在get方法中遇到堆栈溢出异常,c#,asp.net-mvc,C#,Asp.net Mvc,当我重构代码时,当我的视图试图访问我的类的成员时,我的get方法抛出了System.StackOverflowException。 这是我第一次使用??(空合并运算符)在c#中 那是我的型号: using System; using System.ComponentModel.DataAnnotations; namespace Extranet45.Models { public class Complaint { public string Nome

当我重构代码时,当我的视图试图访问我的类的成员时,我的get方法抛出了System.StackOverflowException。 这是我第一次使用??(空合并运算符)在c#中

那是我的型号

using System;
using System.ComponentModel.DataAnnotations;

namespace Extranet45.Models
{
    public class Complaint
    {
        public string Nome
        {
            get => Nome ?? "Nome não informado.";
            private set => Nome = value ?? "Nome não informado.";
        }
        public string Email
        {
            get => Email ?? "Email não informado.";
            private set => Email = value ?? "Email não informado.";
        }
        [Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
        public string Denuncia
        {
            get => Denuncia ?? "O texto da denuncia não foi encontrado.";
            private set => Denuncia = value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
        }
        public Complaint() { }

        public Complaint(string nome, string email, string denuncia)
        {
            Nome = nome;
            Email = email;
            Denuncia = denuncia;
        }
    }
}
using System;
using System.Web.Mvc;
using Extranet45.Models;
using Utils;

namespace Extranet45.Controllers
{
    public class ComplaintController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToAction("Send");
        }

        // GET: Complaint/Send
        public ActionResult Send()
        {

            return View(new Complaint());
        }
    }
}
这是我的控制器/操作

using System;
using System.ComponentModel.DataAnnotations;

namespace Extranet45.Models
{
    public class Complaint
    {
        public string Nome
        {
            get => Nome ?? "Nome não informado.";
            private set => Nome = value ?? "Nome não informado.";
        }
        public string Email
        {
            get => Email ?? "Email não informado.";
            private set => Email = value ?? "Email não informado.";
        }
        [Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
        public string Denuncia
        {
            get => Denuncia ?? "O texto da denuncia não foi encontrado.";
            private set => Denuncia = value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
        }
        public Complaint() { }

        public Complaint(string nome, string email, string denuncia)
        {
            Nome = nome;
            Email = email;
            Denuncia = denuncia;
        }
    }
}
using System;
using System.Web.Mvc;
using Extranet45.Models;
using Utils;

namespace Extranet45.Controllers
{
    public class ComplaintController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToAction("Send");
        }

        // GET: Complaint/Send
        public ActionResult Send()
        {

            return View(new Complaint());
        }
    }
}
这是我的视图中引发异常的部分

@model Extranet45.Models.Complaint

@{

    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Denúncia";
    ViewBag.ShowHeader = false;
    ViewBag.BodyClass = "profile-page"; //Faz com que o header fique com a altura menor, porém ñão é uma solução elegante, refatorar
    ViewBag.HeaderImageUrl = "https://github.com/creativetimofficial/material-kit/blob/master/assets/img/city-profile.jpg?raw=true";

}

@using (Html.BeginForm("Send", "Complaint", FormMethod.Post, new { @Class = "contact-form"}))
{
    <div class="section section-contacts">
        <div class="col-md-8 ml-auto mr-auto">
            @ViewBag.ErrorMessage
            <h2 id="main-title" class="text-center title">Denuncie</h2>
            <h4 class="text-center description">Caso você tenha alguma reclamação ou precise comunicar algo incorreto ao Ceape, nos envie uma mensagem abaixo ou um email para <a href="mailto:denuncias@ceapema.org.br">denuncias@@ceapema.org.br</a></h4>

            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class=" form-group bmd-form-group">
                        <label for="textbox-nome" class="bmd-label-static">Nome - Opcional</label>
                        @Html.TextBoxFor(m => m.Nome, new { @id = "textbox-nome", @Type = "Text", @Class = "form-control", name = "nome" })
                    </div>
                </div>
            </div>

我不知道为什么我使用null合并运算符会导致堆栈溢出。

这是因为您的Nome、Email和Denucia设置程序在一个无限过程中调用它们自己。可以通过为每个属性使用支持字段来解决此问题

   private string _nome ;

   public string Nome
    {
        get => _nome ?? "Nome não informado.";
        private set => _nome = value ?? "Nome não informado.";
    }

    private string _email; 

    public string Email
    {
        get => _email?? "Email não informado.";
        private set => _email= value ?? "Email não informado.";
    }

    private string _denucia;

    [Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
    public string Denuncia
    {
        get => _denucia?? "O texto da denuncia não foi encontrado.";
        private set => _denucia= value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
    }

公共字符串Nome{get=>Nome??“Nome não informado。”
每次你要
Nome
时,它都会不断地要求
Nome
,不断地要求
Nome
,而且……这是因为你的Nome、Email、Denucia的设置者在一个无限的过程中调用自己。顺便说一句,这是一个非常糟糕的主意。如果你写
x.Name=null;
,调用时应该是
null
使用本地化字符串更糟糕。@Groo不知道为什么?巴西本身就是一个大国,并不是所有软件都需要本地化