0

For my ASP.NET MVC webservice I have a model that I return as a JSON object. Now there are some properties in my model that I don't want to return.

An example model:

class Account {
    public int ID { get; }
    public string Username { get; }
    public string Password { get; }
    //... more properties
}

Let's say I only want to return the ID and Username properties as JSON. I am looking for a good way to filter only these properties. Changing the access modifier is not an option for me.

A solution which I could think of is creating a whitelist like below. Here I have added a DisplayName which would be a nice thing to be able to customize, but it isn't required.

class FilterProperty
{
    public string PropertyName { get; }
    public string DisplayName { get; }

    public FilterProperty(string propertyName, string displayName)
    {
        PropertyName = propertyName;
        DisplayName = displayName;
    }
}

class Account
{
    public static FilterProperty[] Whitelist = {
        new FilterProperty("ID", "accountId"),
        new FilterProperty("Username", "accountName")
    };

    //...
}

The downside to this solution is: if I would change the name of a property, I would need to change the whitelist too.

Could I make this work or are there better solutions?

5
  • Check out this article written around under and over posting: odetocode.com/blogs/scott/archive/2012/03/11/…. Especially check out the strongly typed approaches, particularly using interfaces to limit the model. Commented May 20, 2016 at 9:43
  • Can you create AccountDto, map there values you want and just return AccountDto instead of Account? Commented May 20, 2016 at 9:43
  • @hellwd I have tried this, but this doubles the amount of models that I have, which doesn't seem very efficient Commented May 20, 2016 at 9:51
  • Having more models doesn't mean to increase extra burden to your application. Its the only matter how you use them. Commented May 20, 2016 at 9:54
  • Just return an anonymous object containing only those properties you want. Commented May 20, 2016 at 10:13

3 Answers 3

2

There could be multiple solutions to your problem:

Create a ViewModel having the only required properties in it and map those properties from the Original model and return that viewModel. You can use AutoMapper library for mapping the original model to your view model.

The other thing is that ASP.NET Web API uses Json.Net as default formatter, so if your application just only uses JSON as data format, you can use [JsonIgnore] to ignore property for serialization:

class Account {
public int ID { get; }
public string Username { get; }

[JsonIgnore]
public string Password { get; }
//... more properties
}

Hope this will help you out.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good solution for blacklisting properties, but I was hoping to find a way to whitelist them.
1

Web API uses JSON.net as default serializer.

You can add JSONIgnore attribute to skip a property.

 public class Class
      {
      // included in JSON
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

   // ignored
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}

Alternatively if you need to ignore most of the properties you can use opt in approach.

Use DataContract attribute on your class and then add Datamember to only the properties that you want to include

[DataContract] 
public class Class
          {
      [DataMember]
      public string Model { get; set; }
      [DataMember]
      public DateTime Year { get; set; }
      // ignored
      public List<string> Features { get; set; }
      public DateTime LastModified { get; set; }
    }

Comments

0

If you are concerned with naming only, then use of nameof operator is one option. (.NET version dependent)

class Account
{
    public static FilterProperty[] Whitelist = {
        new FilterProperty(nameof(Account.ID), "accountId"),
        new FilterProperty(nameof(Account.Username), "accountName")
    };

    //...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.