Question Response with double quote

Madlosft

New member
Joined
May 25, 2023
Messages
1
Programming Experience
1-3
Greetings to everyone and always thanking you for your help, I am developing a RestApi application in the .NET Framework 4.8 on the C# language and for this I need to respond in my response from my service a text that includes double quotes, this text comes from the database.

Text: {"title": "Dear User", "content": "You currently have no information."}

DTO:
///DTO db y de salida

public class ParametroDto

    {

        public ParametroDto() { }

        public string codParametro { get; set; }

        public string glsParametro { get; set; }

        public string valParametro{get; set; }

    }

I have made a replace to be able to solve it but I have not obtained a response that shows me the same message that is found in the database.

Response service:
if (_consulta == null)

                {

                  

                    List<ParametroDto> mensaje = ServiceConsultaSeguro.Instance.ConsultaSeguroParametro("SININF");

                    consulta.mensaje = new List<ParametroDto>();

                    foreach (ParametroDto parametro in mensaje)

                    {

                        parametro.valParametro = "$" + parametro.valParametro.Replace("\\\\"," ");

                        //parametro.valParametro = parametro.valParametro.Replace(@"\\", "");

                

                        consulta.mensaje.Add(parametro);

                    }



                    return consulta;

                }



                consulta.seguros = consultaSeguro;

Hope I get help on this topic
 
If you are using a JSON library as a serializer, it should automatically escape and double quotes for you. You don't need to do any of the escaping yourself before seralization.
 
Indeed, for example:

C#:
[Route("api/[controller]")]
[ApiController]
public class SomeController : ControllerBase
{
    
  [HttpGet]
  public async Task<ActionResult> GetSomething()
  {
    return Ok(new { SomeText = @"She said ""hello"" to me" });
  }
}

.net will do the rest

Note, to be clear you don't have to do anything in your code, like replacing or doubling quotes up; my code there contains "" because that is how you hard code a string so that the compiler puts quotes into the string. Your string from the db already contains a quote char. Just return it.
 
Back
Top Bottom