I am trying to send list of string to web api and this list I am initializing in class constructor and same class I am passing to web api.

When I am initializing the class; list is getting populated properly but after sending it to web api same items are getting added again into the list. Not sure why this happening.

As I am accepting these values from client; If no value are provided by the client then I want to fill the default value into the list.

Below is my code

public class RequestDto
    {
        public RequestDto()
        {
            TypeNames = new List<string>();
            TypeNames.Add(new string("Type1"));
            TypeNames.Add(new string("Type2"));
            TypeNames.Add(new string("Type3"));

        }

        public string CompanyName { get; set; }
        public string Version { get; set; }
        public List<string> TypeNames { get; set; }
    }

and then I am passing the class to web api like below

using (var client = new HttpClient())
            {
                string url = "some url";

                var postTask = client.PostAsJsonAsync(url, objrequestdto);
                postTask.Wait();

                if (postTask.Result.IsSuccessStatusCode)
                {
                    // Do Something
                }
            }

and below is my api where after receiving the parameter I can see the items are getting added again.

[HttpPost]
        public IActionResult GetTypeDetails([FromBody] RequestDto requestdto) // Here I am getting duplicate/Repeated Values
        {
            try
            {
                // Some logic

            }
            catch (Exception e)
            {
                Log.Error("Some Error");
                return StatusCode(500, e.Message);
            }
        }

This is how I am populating RequestDto class before calling web api.

EDIT 1:

RequestDto objrequestdto = GetConfigurations(configName);

private RequestDto GetConfigurations(string configName)
        {

            RequestDto requestdto = new RequestDto();

            /// Add configurations here.

            return requestdto;
        }

Because of this I am getting duplicate records in Web API.

Any help on this appreciated!

3 Answers

1
Owen Pauling On

This is because you're adding the values in the constructor.

0
cem On

You're using default constructor for adding the default values, which is also used by Serializer. Serializer will call your default constructor first, and then it'll populate the items from the JSON.

To prevent duplication, you could use ISet<> instead of List<>.

public class RequestDto
{
    public RequestDto()
    {
        TypeNames = new HashSet<string>();
        TypeNames.Add(new string("Type1"));
        TypeNames.Add(new string("Type2"));
        TypeNames.Add(new string("Type3"));
    }

    public string CompanyName { get; set; }
    public string Version { get; set; }
    public ISet<string> TypeNames { get; set; }
}
0
XamDev On

If anyone looking for the answer, I have resolved it by adding below json formatting in Startup.cs file in Web API project.

services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
                options.SerializerSettings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
            });

This has resolved the issue for me.