Say my uri expects GUID whereas client is supplying string.empty.
I would like to have control on the error message being send to the client.
The default error message is like this -
<Error>
<Message>
The request is invalid.
</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Guid' for method 'xx.xx.MyResponse Get(System.Guid)' in
'xx.xx.MyController'. An optional parameter must be a reference type, a nullable
type, or be declared as an optional parameter.
</MessageDetail>
</Error>
But what if I want to throw the custom error like this -
<Error>
<Message>
Invalid GUID.....Please enter a valid GUID.
</Message>
</Error>
Here is the REST Method definition -
public MyResponse Get(Guid id)
{
// method body
}
I also implemented custom exception filter but it didn't reached to it -
public class ResponseExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(statusCode, "Invalid GUID....Please enter a valid GUID.");
}
}
it didn't reached to OnException and throws system error....which I don't want.
Now what if the client passed "abcd" instead of a valid guid, it should display my own exception instead of system defined exception.