Click here to Skip to main content
15,609,649 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am building a rest API to mobile apps developers. I want them each time they call my APIs to check first if the returned status is 200 or something else. So I googled and found this link which is handling status codes of rest API asp.net:
create Web API response[^]
But my Rest API is already built an another way with 115 webmethods and I cannot change all my methods and make them like this. Below is an example of how my webmethods are built:

Public Function InfoAndTerms(ByVal Lang As String) As Information() Implements IService.InfoAndTerms

Dim result() As Information    
Try
    ' Do something and fill result
Catch ex As Exception
    ' Do something else
Finally  
        InfoAndTerms = result
End Try   

End Function


Where 'InfoAndTerms' is a Structure in a 'DataTypes' module

So my question is: isn't there any other way to return http status code with object as response?
Your helps are appreciated

What I have tried:

I have tried this :

Public Function InfoAndTerms(ByVal Lang As String) As Information() Implements IService.InfoAndTerms

Dim result() As Information    
Try
    ' Do something and fill result
Catch ex As Exception
    Throw New System.Web.HttpException(500, "Error - InfoAndTerms")
Finally  
        InfoAndTerms = result
End Try   

End Function


But when I tested this method I got status 400 instead of 500. SO what could be the problem?
Posted
Updated 3 days ago

1 solution

Finally I found a solution:

According to this link we just have to use 'WebFaultException' which will change the http status. Now there is also a nice method to return the handled error :

Public Function TestMethod2(ByVal name As String) As String Implements IService.TestMethod2

        If name = "" Then

            Dim str As New ErrMessage
            str.intErr = 1
            str.strErrMessage = "bla bla bla"

            Throw New WebFaultException(Of ErrMessage)(str, HttpStatusCode.BadRequest)

        End If

        Return name

    End Function


Then the status will change and the returned object will be an error. Personally I prefer to return object error instead of anything else ...

Cheers :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900