14

How do I exclude certain properties, or explicitly specify which model properties should be bound by Web Api model binder? Something similar to CreateProduct([Bind(Include = "Name,Category") Product product) in ASP.NET MVC, without creating yet another model class and then duplicating all the validation attributes for it from the original model.

// EF entity model class
public class User
{
    public int Id { get; set; }       // Exclude
    public string Name { get; set; }  // Include
    public string Email { get; set; } // Include
    public bool IsAdmin { get; set; } // Include for Admins only
}

// HTTP POST: /api/users | Bind Name and Email properties only
public HttpResponseMessage Post(User user)
{
    if (this.ModelState.IsValid)
    {
        return this.Request.CreateErrorResponse(this.ModelState);
    }

    this.db.Users.Add(user);
    this.db.SaveChanges();
    return this.Request.CreateResponse(HttpStatusCode.OK));
}

// HTTP POST: /api/admin/users | Bind Name, Email and IsAdmin properties only
public HttpResponseMessage Post(User user)
{
    if (!this.ModelState.IsValid)
    {
        return this.Request.CreateErrorResponse(this.ModelState);
    }

    this.db.Users.Add(user);
    this.db.SaveChanges();
    return this.Request.CreateResponse(HttpStatusCode.OK));
}
0

2 Answers 2

1

If you're using JSON, you can use the [JsonIgnore] attribute to decorate your model properties.

public class Product
{
    [JsonIgnore]
    public int Id { get; set; }          // Should be excluded
    public string Name { get; set; }     // Should be included
    public string Category { get; set; } // Should be included
    [JsonIgnore]
    public int Downloads { get; set; }   // Should be excluded
}

For XML, you can use the DataContract and DataMember attributes.

More info about both at the asp.net website.

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

2 Comments

This way I won't be able to specify inclusion/exclusion list per action method. For example, there could be a different set of properties to include for the same model which is used in different action methods.
I haven't tested this, but maybe creating your own ModelBinder by implementing IModelBinder may get you to your solution. That way you can create an attribute similar to what Bind does and attach it to the paramater for your action. link.
0

You can try the Exclude attribute.

So now your class look something like this -

public class Product
{
    [Exclude]
    public int Id { get; set; }          // Should be excluded
    public string Name { get; set; }     // Should be included
    public string Category { get; set; } // Should be included
    [Exclude]
    public int Downloads { get; set; }   // Should be excluded
}

1 Comment

This is specific to WCF

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.