Return exception to client

Topics: Web Api
Sep 5, 2011 at 8:25 PM

Hey, sorry for my bad english...

I follow the sample on quickstart (http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api) and now have a simple service exposing some json data.
When someone make a request to my url, they have to pass a code on the url. I need to validate this code on a database and  if it is valid return my data (as Queryable) but if it is invalid, i have to return an erro to the client (just a string would be fine). How can I acomplish this? Any Exception or HttpException that i throw on the server give me a http 500 error:

The server encountered an error processing the request. See server logs for more details

 

Coordinator
Sep 5, 2011 at 11:59 PM

Have you tried throwing HttpResponseException<T>? It will let you provide an HttpResponseMessage which is returned to the client.

Thanks
Glenn

Sep 6, 2011 at 6:16 AM

Well, i feel so ashamed :( ... after spending too much time on silverlight/ria/mvvm I forgot that I could just Response.Write something to show a string on the client :/

But your sugestion made the end result better, since i can control the http code of the request. Probably what I end up doing (Response.Write the string and throwing the HttpResponseException) is not the cleanest way but it gets the problem out of my way for now :D (or did I miss something and there is a way to capture the reasonPhrase of the HttpResponseMessage on the client?)

Thanks for the fast answer :)

 

leo

Sep 6, 2011 at 5:13 PM

what namespace and dll is HttpResponseException<T> defined?  I can only reference the non-generic type.  

Sep 6, 2011 at 6:00 PM

It would be very odd if they were to be in different namespaces.
I am using Preview 4 and I do not find the generic one either.
I guess they added it recently and it should be in the next preview.

Sep 6, 2011 at 6:54 PM

I didn't found it either, just used the non-generic type...

Sep 6, 2011 at 6:59 PM

Is it in the extesions?

On Sep 6, 2011 12:00 PM, "santiagoIT" <notifications@codeplex.com> wrote:
> From: santiagoIT
>
> It would be very odd if they were to be in different namespaces.
> I am using Preview 4 and I do not find the generic one either.
> I guess they added it recently and it should be in the next preview.
>
>