Andy
Andy

Reputation: 856

Receiving different responses depending on client

I have two ASP.NET MVC web apps running on the same server. One of them is a web service that returns an error message in plain text if an exception occurs. However, right now, some clients that call the web service don't receive the error message; instead, they simply receive "Bad Request" in HTML.

The second web app (on the same server as the first) can call a URL handled by the first one and, right now, correctly receives the error message in plain text. However, I have tried calling that URL other ways, and all of them have resulted in receiving "Bad Request":

This error does not occur locally. When I run the 2 web apps on my computer, I receive the error message in plain text from both the second web app and from calling the local URL from Chrome.

I have narrowed down the offending line of code to the first line of the following ActionResult snippet:

Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content(errorMessage, ContentTypes.PlainText);

Removing the first line appears to fix the problem; however, that also eliminates the ability for me to use a descriptive status code. It appears to me that after the ActionResult is returned the response is being intercepted if either (a) the client is on a different computer or (b) the client is a web browser. So I guess I have a 2-part question:

  1. Is there a reason why .NET or IIS would intercept and change a response depending on the client type or location?
  2. Is there an easy way to view the response at any point between this code and when it's dispatched to the client?

Thanks!


Update: I changed the web app to use HttpResponseException. Now I am getting the following YSOD exception:

Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.

Using MVC version 5, Visual Studio 2013. The code for the ActionResult looks like this:

MyImage image = new MyImage(parameters);

if (image.Errors.Any())
{
    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(image.Error) });
}

return File(image.AsJpeg(), ContentTypes.Jpeg);

Anyone have an idea how to bypass this unhelpful response?


Update 2: The issue turned out to be that the error message was being suppressed because of the Web.config setting system.webServer > httpErrors > errorMode which has a default value of "DetailedLocalOnly" and seems to be invoked in some cases for a reason I don't know (although this question may start to shed some light). Once I changed it to this, it worked as I expected:

<httpErrors errorMode="Detailed" />

I understand why they suppress error messages by default on remote machines, but this was a lot harder to track down than I would have thought. Anyway, I hope this is helpful to someone in the future.

Upvotes: 2

Views: 800

Answers (1)

Always Learning
Always Learning

Reputation: 2713

I can't think of any reason why IIS would care what client was calling a service. My guess is that the client is sending a different request to the server than what you think it is sending. You can verify this by using a program called "Fiddler".

Also, I'd recommend following a pattern that returns a HttpResponseMessage like this when sending back information from a Web API call:

return new HttpResponseMessage(HttpStatusCode.BadRequest)
        {
            ReasonPhrase = message,
            Content = new StringContent(string.Format("{0}", exception))
        };

Upvotes: 1

Related Questions