Click here to Skip to main content
14,027,355 members
Rate this:
 
Please Sign up or sign in to vote.
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 12-Jun-17 19:03pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 :)
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190419.4 | Last Updated 7 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100