Code Review - Is this Ok?
Web Development
1
Posts
1
Posters
2
Views
1
Watching
-
I have a .Net Frameworkk API Controller that I'm calling from a WPF client:
[HttpGet]
public HttpResponseMessage GetPersonList(int personTypeId = 0, int clientRepId = 0)
{
var response = new HttpResponseMessage();try { var data = Repository.GetPersonList(personTypeId, clientRepId); response = Request.CreateResponse(HttpStatusCode.OK, data); } catch (Exception e) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return response;
}
Client Side
namespace APITester
{
internal class Program
{
static string baseURL = @"https://localhost:44381";private async static Task<List<PersonEntity>> GetPersonList() { List<PersonEntity> results = null; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(baseURL); var uri = "/api/Person/GetPersonList?clientRepId=0&personTypeId=0"; HttpResponseMessage response = await client.GetAsync(uri); var contents = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { results = JsonConvert.DeserializeObject<List<PersonEntity>>(contents); } else { // Deserialize the error into an ErrorDetails class var error = response.Content.ReadFromJsonAsync<ErrorDetails>().Result; // Do something here } } return results; } } public class ErrorDetails { public string ExceptionMessage { get; set; } public string ExceptionType { get; set; } public string Message { get; set; } public string StackTrace { get; set; } }
}
Everything works fine. Just curious if this the right way.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.