locked
Httpresponsemessage or var is best RRS feed

  • Question

  • User-148788041 posted
    I am using post method. I use var st=new httpclient().postasync(url,content);
    When service is down it says one or more error.
    I need to say server is not responding message as shown in postman.
    Wednesday, May 29, 2019 8:12 AM

All replies

  • User475983607 posted

    I am using post method. I use var st=new httpclient().postasync(url,content);
    When service is down it says one or more error.
    I need to say server is not responding message as shown in postman.

    This is a duplicate thread.

    https://forums.asp.net/t/2156068.aspx?Httpresponsemessage+or+var+is+best

    https://forums.asp.net/t/2156080.aspx?When+API+not+responding+httpResponse+message+throws+error

    Basically, the client is responsible for catching the exception and displaying a message.  

    Declaring st as var or HttpResponseMessage is irrelevant.  The var type is implicitly determined by the compiler.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    Wednesday, May 29, 2019 1:02 PM
  • User61956409 posted

    Hi Guhananth,

    I am using post method. I use var st=new httpclient().postasync(url,content);
    When service is down it says one or more error.
    I need to say server is not responding message as shown in postman.

    If you'd like to catch the exception and display "server is not responding" message in your application, please use try-catch statement with your code logic.

    If you want to display specific message when the service is unavailable, you can check the response status code like below.

    if (st.StatusCode== HttpStatusCode.ServiceUnavailable)
    {
        //code logic here
    }

    Besides, please do not post similar/same questions in multi threads. Thanks for your understanding.

    With Regards,

    Fei Han

    Thursday, May 30, 2019 2:33 AM