0

I have an API for storing data and I'm consuming it via my mvc application. The authentication process, storing the data and everything else is working properly. The thing is that I am trying to handle some errors or exceptions that may occur in the future. One of them is handling if my application fails to connect to to my web api and throwing an error or leading the user somewhere else.

My web application controller which consumes the api:

HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("https://localhost:44304");
            HttpContent content = new FormUrlEncodedContent(
            new List<KeyValuePair<string, string>> 
            {
                new KeyValuePair<string, string>("username", lgv.username),
                new KeyValuePair<string,string>("password", lgv.password),
                new KeyValuePair<string,string>("grant_type","password"),
            });
        content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        content.Headers.ContentType.CharSet = "UTF-8";

        /*this is the line where all the parameters get passed and validates everything*/
        var resultDisplay = await client.PostAsync("token", content);
        /* **************************************************************************** */
        if (resultDisplay.IsSuccessStatusCode)
        {
            var loginData = await resultDisplay.Content.ReadAsAsync<LoginResponseViewModel>();
            Session["access_token"] = loginData.Access_Token;
        }

So what I have tried is:

HttpException hc = new HttpException();
switch(hc){
case 200: return view("success"); /* since successful code is 200*/
break;
case 400: return view("error");
break;
}

But it doesn't seem to work because this code only checks the state of the application and not of the api... So my question is, how can I handle it if my api is not running?

  • Where did you have the exception code? Could you show the whole code, pls – Serge Aug 27 '21 at 14:54
  • Exception code till now is just to handle after the user is accessed to this api, it just handles users input if it is null or not/ if it exists on database and stuff like that. I thought it was not necessary and that's why I didn't showed up them. –  Aug 27 '21 at 14:56

1 Answers1

0

So instead of making the switch on the application make it over resultDisplay.StatusCode instead, this way you are checking the API state using the response status code.

Like this:

switch(resultDisplay.StatusCode){
case 200: return view("success"); /* since successful code is 200*/
break;
case 400: return view("error");
break;
}
Eman
  • 56
  • 6
  • I cannot leave it like ` case 200: ...` due to error "Cannot implicitly convert type int to System.Net.HttpStatusCode". Also putting the status code in double quotes or single quotes didn't worked either. What should I try next? –  Aug 30 '21 at 07:09
  • You can use HttpStatusCode.BadRequest instead of the string or any other status code you want – Eman Aug 31 '21 at 11:14