How to Hide a Property in Webapi

How can we hide a property in WebAPI?

I just figured out

[IgnoreDataMember]
public int DeviceId { get; set; }

The namespace is System.Runtime.Serialization

More information IgnoreDataMemberAttribute Class

Learnt something new today.

Thanks All.

Hide model classmember in controller in C# Web API?

You could create a custom DTO as a view model for the POST operation on this controller. This would be additionally handy because you probably also don't want the client to supply the Id value either (I assume). Something like this:

public class PersonDTO
{
public string Name { get; set; }
public string Title { get; set; }
}

This would be the input for the controller action:

public void PostPerson(PersonDTO person)
{
// Upload person to database
}

Then in the code you'd create a new PersonModel to add to the data context. Something like:

using (var db = new MyDataContext())
{
var newPerson = new PersonModel
{
Name = person.Name,
Title = person.Title
};
db.Persons.Add(newPerson);
db.SaveChanges();
}

(Or perhaps create a kind of translation method on the DTO which returns an instance of the model, acting as a sort of factory method and putting the logic in the object rather than in the controller.) This way the client isn't providing an entire PersonModel instance, just an object which describes the creation of that instance. The GET operation can still return the full PersonModel.

When building an API (using WebAPI, for example) it can often be really useful to fine-tune the inputs and outputs like this. And such custom DTOs/ViewModels really come in handy, albeit at the cost of slightly more code by creating essentially a translation layer to the backing models.

One tool I've found particularly handy in determining where in the API I need to tweak things is when using Swagger to generate my API docs. Looking through the generated docs, I may notice something which I don't want to be exposed. This is an indicator that I need to customize that API endpoint a little more so that the resulting docs are a little cleaner.

ASP.Net Web API Help Pages: Ignore certain properties

No, there isn't a similar option for a property. HelpPage uses formatter instances configured on the application to serialize the samples and as you can imagine the formatters must not have this knowledge within themselves.

Regarding workarounds:

a. You could explicitly set the raw sample for a particular action's requestsample via the SetSampleRequest extension of HttpRequestMessage. You should be able to see some examples about this in the file at *Areas\HelpPage\App_Start\HelpPageConfig.cs*.

b. In the file Areas\HelpPage\SampleGeneration\HelpPageSampleGenerator.cs, there is a method called WriteSampleObjectUsingFormatter which uses the application's formatter instances to write the samples. Here you would need to create new formatter instances having similar settings as your normal application has(so that they reflect the exact serialization/deserialization semantics that your application would normally react to when actual requests are made) and then try to hide the properties which you want to. We want to create new instances because we do not want to disturb the normal functioning of the application.

Example: In case of Json, you could create a new Json formatter instance and provide a ContractResolver which can hide the properties. Check this link: http://james.newtonking.com/projects/json/help/html/ConditionalProperties.htm

In case of Xml, I am not sure how we can hide properties without using the IgnoreDataMember attribute and also being non-intrusive.

Currently I would prefer option 'a' as its comparatively a simple workaround than 'b'.



Related Topics



Leave a reply



Submit