1

I've written an ASP.Net web API, my requirement to show the full/some result (s) JSON based on the parameter i.e., verbose=true

To Explain this requirements.

My current JSON is

Without verbose

GET Method:

api/v1/patient?Key=1

{
    "user": {           
            "key": 1,
            "suffix": "1",
            "firstName": "Dhanu",
            "lastName": "Kumar",
            "middleName": "",
            "address": {
                "address1": "uuu",
                "address2": "TTT",
                "address3": "xx",
                "city": "yy"           
            }
        }
}

With verbose

api/v1/patient?Key=1&verbose=true

{
    "user": {           
            "key": 1,
            "firstName": "Dhanu",
            "lastName": "Kumar",
            "middleName": ""
        }
}

My User.cs

public UserDTO()
{
    public int Key { get; set; }
    public string Suffix { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public Address Address {get;set;}       
}

Based on the verbose parameter, I'll Hide/Show some fields from the JSON.

Is there any way to achieve this?

4
  • Try using inheritance and have the endpoint return the type based on provided parameter. Commented May 22, 2018 at 16:23
  • @Nkosi Is there any other way without creating a new Class? I mean some annotation/quick way to denote that fields alone in the same class to show based on the verbose value ? Commented May 22, 2018 at 16:33
  • None that I know of. Commented May 22, 2018 at 16:34
  • I mean you could always manually construct the response message...that will be a lot more work though Commented May 22, 2018 at 23:08

1 Answer 1

4

You can use inheritance

public class UserDTO {
    public int Key { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }    
}

public class VerboseUserDTO: UserDTO {
    public string Suffix { get; set; }
    public Address Address {get;set;}       
}

and have the endpoint return the type based on provided parameter.

//api/v1/patient
public IHttpActionResult Get(int key, bool verbose = false) {
    //...get data based on key

    if(data == nul)
        return NotFound();

    if(verbose) {
        var verboseDto = new { 
            user = new VerboseUserDTO {
                //...populated from data
            }
        };
        return Ok(verboseDto);
    }

    var dto = new { 
        user =  new UserDTO {
            //...populated from data    
        }
    };    
    return Ok(dto);
}
Sign up to request clarification or add additional context in comments.

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.