Exceptions returned as plain text description of the http error code

My response class is

public class CreateXXXResponse : IHasResponseStatus
{
    public ResponseStatus ResponseStatus { get; set; }
    public XXX Result { get; set; }
}

When I there is no Exception, all is returned as a nice json with Result properly rendered.

However, when there is an exception, say BadRequest, the API returns just “BadRequest” plaintext, without any json though ResponseStatus is populated properly - I have put a breakpoint into the class.

What could be the cause?

The app is ASP.NET MVC with ServiceStack running the API.

Please post the raw HTTP Request and Response using something like Chrome Web Inspector or Fiddler.

The models are these:

[Route("/apiversion", "GET")]
public class GetApiVersion : IReturn<GetApiVersionResponse>
{
}

public class GetApiVersionResponse : IHasResponseStatus
{
    public ResponseStatus ResponseStatus { get; set; }
    public string Version { get; set; }
}

The service is

public class ApiVersionService
{
    public object Get(GetApiVersion request)
    {
            throw new ArgumentException("Wrong Result");
    }
}

The request logs:

Request URL:http://localhost:7132/api/apiversion
Request Method:GET
Status Code:400 ArgumentException
Remote Address:[::1]:7132
Referrer Policy:no-referrer-when-downgrade

Response Headers

HTTP/1.1 400 ArgumentException
Cache-Control: private
Content-Type: text/html
Vary: Accept
Server: Microsoft-IIS/10.0
X-Powered-By: ServiceStack/4.512 NET45 Win32NT/.NET
X-Startup-Errors: 1
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcRHJvcGJveCAoUm9ja2V0RWRnZS5jb20pXHNyY1xDbGllbnRzXE15TWVjaGFuaWMubWVcTXlNZWNoYW5pYy5Eb3ROZXRcTXlNZWNoYW5pYy5XZWJcTXlNZWNoYW5pYy5XZWJcYXBpXGFwaXZlcnNpb24=?=
X-Powered-By: ASP.NET
Date: Tue, 27 Jun 2017 13:31:59 GMT
Content-Length: 11

Request Headers

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8,de;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:ss-id=WvpfQmGUitQqmnx63GXb; ss-pid=4hZ6pSzclHbrL36hwzRp
DNT:1
Host:localhost:7132
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Response:

Bad Request

Your ApiVersionService class should inherit Service but I’m assuming it already does and you’re not providing the exact implementation.

I’m not sure why you’re a plain text ‘Bad Request’ response, as the request is being called from a Browser so you should be seeing a HTML response. I’m assuming you have some other configuration that’s interfering with the response. If you can put a small stand-alone example on GitHub I can investigate further and identify the issue.

Yes, the Service is inherited via another class.

I found the cause. For the ASP.NET MVC we require redirect of all 404 and 500 errors to a specific website (done via web.config)

it apparently interferes with ServiceStack’s error handling.

        <httpErrors errorMode="Custom" existingResponse="Replace">
            <clear />
            <error statusCode="404" path="http://XXX.com" responseMode="Redirect" />
            <error statusCode="500" path="http://XXX.com" responseMode="Redirect" />
        </httpErrors>

Can you see a way how to get it work with ServiceStack API? The app is ASP.NET MVC with ServiceStack’s provided API.

The issue is MVC hijacking the Error Responses which happens outside of ServiceStack. Have a look at the Form Hijacking Prevention docs for how you can prevent some errors responses being hijacked.

Ha! This is most useful! Thank you!