Web API controller to get details of a travel plan or a leave request My web API has 3 controllers for get, post and users CRUD. I named it as DetailsControllor, FormControllor and AuthControllor. In the details controllor, I used a switch case to find out which type of details needed, but I found that code is repeated in switch case. Can I use generics to minimize code or is there any other way to carry out these things?

 [HttpPost]
    [Route("details/{id}")]
    public async Task&lt;HttpResponseMessage&gt; GetDetails(string id)
    {


        try
        {
            var content = Request.Content.ReadAsStringAsync().Result;
            HttpResponseMessage response = Request.CreateResponse();
            switch (id)
            {
                case "leave":
                    RequestBase LeaveInput = jsonSerializer.Deserialize&lt;RequestBase&gt;(content);
                    Task&lt;LeaveForm&gt; LeaveOutput = repo.GetLeaveRequest(LeaveInput);
                    response.Content = new ObjectContent(typeof(object), LeaveOutput, jsonFormatter);                       
                    break;

                case "travel":
                    RequestBase travelInput = jsonSerializer.Deserialize&lt;RequestBase&gt;(content);
                    Task&lt;BaseTravel&gt; travelOutput = repo.GetTravel(travelInput);
                    response.Content = new ObjectContent(typeof(object), travelOutput, jsonFormatter);
                    break;


            }
            return response;
        }
        catch (Exception e)
        {

             HttpResponseMessage responseToClient = Request.CreateResponse((HttpStatusCode)422);
             responseToClient.Content = new StringContent(e.message, Encoding.Unicode);
         throw new HttpResponseException(responseToClient);  
        }
    }
I'd re-think your switch:


RequestBase LeaveInput = jsonSerializer.Deserialize&lt;RequestBase&gt;(content); is repeated, the only difference it is assigned to a different variable depending on the case. 
response.Content = new ObjectContent(typeof(object), travelOutput, jsonFormatter); is also repeated, with the only difference being the second parameter. 


In fact, the three lines of each case are virtually identical, except for repo.GetLeaveRequest() and repo.GetTravel(), which suggests to me that the switch should happen at a lower level.

Perhaps you could do something like this:

private ObjectContent GetObjectContent(string id, string content)
{
    var input = jsonSerializer.Deserialize&lt;RequestBase&gt;(content);

    object output;
    switch (id)
    {
        case "leave":           
            output = repo.GetLeaveRequest(LeaveInput);
            break;

        case "travel":
            output = repo.GetTravel(travelInput);
            break;
    }

    return new ObjectContent(typeof(object), output, jsonFormatter);
}


Thus the contents of your try could become:

var content = Request.Content.ReadAsStringAsync().Result;
var response = Request.CreateResponse();
response.Content = GetObjectContent(content);
return response;


Note that I'm not sure this works, consider it more like an idea than workable code. (I just don't feel like starting up VS to test this.)



Just a couple of quick remarks:


Local variables like LeaveInput and LeaveOutput should be camelCase.
You explicitly use RequestBase and HttpResponseMessage and Task&lt;LeaveForm&gt;: why not use var like elsewhere in your code?