-3

I need to create a method called publish, to get the name, description, endpoint, no of operands and operand type in JSON format and write it to a text file. I have implemented that part but after the first API call the text in the text file is getting over written which is not what I want. I have included the model class and the controller class below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

namespace Registry.Models
{
    public class publishModel
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string endpoint { get; set; }
        public int NoOfoperands { get; set; }
        public string operandType { get; set; }
    }
}


using Newtonsoft.Json;
using Registry.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Web.Http;

namespace Registry.Controllers
{
    public class publishController : ApiController
    {

        [Route("api/publish")]
        [HttpPost]
        public string publish(publishModel pModel)
        {
            string servicePath = @"C:\Users\ASUS\source\repos\DC assignment 1\Services.txt";


            MemoryStream ms = new MemoryStream();
            var write = new Utf8JsonWriter(ms);

            write.WriteStartObject();
            write.WriteString("Name", pModel.Name);
            write.WriteString("Description", pModel.Description);
            write.WriteString("Endpoint", pModel.endpoint);
            write.WriteString("No of operands", pModel.NoOfoperands.ToString());
            write.WriteString("Operand type", pModel.operandType);
            write.WriteEndObject();
            write.Flush();

            string json = Encoding.UTF8.GetString(ms.ToArray());
            //string json = JsonConvert.SerializeObject(pModel);

            try
            {
                using (StreamWriter writer = new StreamWriter(servicePath))
                {
                    writer.Write(json);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return "Description saved";
        }

     
        
    }
}
  • 1
    What are you actually asking for? Generally, to append to a file, you have to create the `StreamWriter` with the correct flag. See https://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter. But, if you're writing JSON, it's not likely to be useful to append new JSON to a file that already has JSON in it, or has anything else in it for that matter. – Peter Duniho Apr 25 '21 at 22:44

1 Answers1

-1

https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=net-5.0#System_IO_StreamWriter__ctor_System_String_System_Boolean_

Add an extra argument to your StreamWriter:

using (StreamWriter writer = new StreamWriter(servicePath, true))

That tells it you open the path for writing, but append to the file. When you don't pass in a Boolean (which is what you are doing) it defaults to overwriting the file.

B.O.B.
  • 704
  • 4
  • 10