2

I have a WCF REST service whose interface methods are annotated with [WebGet(..., ResponseFormat = WebMessageFormat.Json)] and normally provide JSON-formatted responses. Right now the code indicates errors with:

throw new WebFaultException<string>("helpful message",HttpStatusCode.BadRequest);

The problem is that the Content-Type on the response is still application/json even though the body is just plain text, not JSON encoded. I can make a helper to generate my fault exceptions that will set WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";, but if there's a quick fix in the WCF layer that would just set the content type for these types of exceptions that would be preferable. What's the cleanest way to do this?

4

1 に答える 1

0

WebFaultException を継承してプロジェクトで使用できます。

public class MyWebFaultException<T>:WebFaultException<T>
{
    public MyWebFaultException(T message)
        : base(message, HttpStatusCode.BadRequest)
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
    }
}
于 2012-07-31T02:29:15.323 に答える