I understand that the API could be returning status codes like this:
200 - OK
400 - Bad Request
500 - Internal Server Error
Here is my code, how to handle the error from API in my controller?
public ActionResult Get()
{
string token_access = User.Identity.GetAccessToken();
Uri apiHistoryUri = new Uri(new Uri(ConfigurationManager.AppSettings["ida:ApiUri"]), "api/student/name");
var client = new RestClient(apiHistoryUri);
var request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", token_access);
IRestResponse response = client.Execute(request);
var content = response.Content;
return View(content);
}
The response from API can be anything, how to handle the exception here.
IRestResponse response = client.Execute(request);
var content = response.Content;
What I have tried:
I tried to do this way, but im not sure.
IRestResponse response = client.Execute(request);
try
{
var content = response.Content;
return View(content);
}
catch {
throw (new Exception(res.StatusDescription.ToString()));
}