In my WCF REST service, there is a method GetUser(username), which will
throw new WebFaultException<string>("there is no this user", HttpStatusCode.NotFound);
In my asp.net client, I want to catch above exception and show “there is no this user” on a label. But when I try coding as follow:
MyServiceClient client = new MyServiceClient;
try
{
client.GetUser(username);
}
catch (Exception ex)
{
Label.Text = ex.Message;
}
It turns out show the message “NotFound” instead of “there is no this user”.
How can i do to show the message “there is no this user”?
20/4
In my REST service:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
UriTemplate = "{username}")]
void GetUser(username);
.svc class :
public void GetUser(username)
{
try
{
Membership.GetUser(username);
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
}
catch (Exception ex)
{
throw new WebFaultException<string>("there is no this user", HttpStatusCode.NotFound);
}
}
If you look into the documentation, it’s obvious that you should be showing the
Detail, not theMessage. Your line should be:EDIT: changed exception type
Also, you should be catching the specific exception (
WebFaultException<string>) that you’re interested in, not any-old exception that happens to be thrown. Especially, as you’ll only have theDetailon theWebFaultException<string>type, it’s not onException.See the WebFaultException Class