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<HttpResponseMessage> GetDetails(string id)    {        try        {            var content = Request.Content.ReadAsStringAsync().Result;            HttpResponseMessage response = Request.CreateResponse();            switch (id)            {                case leave:                    RequestBase LeaveInput = jsonSerializer.Deserialize<RequestBase>(content);                    Task<LeaveForm> LeaveOutput = repo.GetLeaveRequest(LeaveInput);                    response.Content = new ObjectContent(typeof(object), LeaveOutput, jsonFormatter);                                           break;                case travel:                    RequestBase travelInput = jsonSerializer.Deserialize<RequestBase>(content);                    Task<BaseTravel> 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);          }    }
Web API controller to get details of a travel plan or a leave request
c#;json;api;generics;controller