简体   繁体   中英

Core WebAPI not returning all values/parameters

I am upgrading a .NET 4.5 solution to.Net 5. In the process of this i have found some code that works in 4.5 but does not seem to work in Core. It is a rather simple WebAPI call, a simplified example as follows;

Model:

public class UJ
{
    public interface IJComponent
    {
        string Type { get; set; }
    }

    public class JComponentRte : IJComponent
    {
        public string? Type { get; set; }
        public string Text { get; set; }
    }
}

Controller:

private List<IJComponent> GetJComponents(...)
{
    var sComponents = new List<IJComponent>();
    foreach (var component in components)
     {
        sComponents.Add(
                        new JComponentRte { 
                            Type = "My type", 
                            Text = "My text" 
                        });
                    break;
    }
}

The issues is that it always just returns the Type, not the Text. The Text is not just empty, is is not included in the return value at all - like it is only looking at IJComponent as the Model and not the JComponentRte? WHat am I missing?

Am not sure why it would have worked in earlier version but problem appears to be that your interface does not include the Text property, so when the result is being serialised using interface IJComponent, there is no Text property to serialise.

Either add the Text property to the interface:

public interface IJComponent
{
    string Type { get; set; }
    string Text { get; set; }

}

Or return a list of JComponent:

private List<JComponent> GetJComponents(...)
{
    var sComponents = new List<JComponent>();
    foreach (var component in components)
     {
        sComponents.Add(
                        new JComponentRte { 
                            Type = "My type", 
                            Text = "My text" 
                        });
                    break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM