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":
Pasting the URL into Chrome on my computer
Pasting the URL into IE on the server
Calling the URL from a web app on a different computer from the server
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:
Is there a reason why .NET or IIS would intercept and change a response depending on the client type or location?
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.
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))
};
Related
Currently, our ASP.NET Core 3.1 web api's error handling sends everything to our error controller using app.UseExceptionHandling('/error'). This controller logs the error and sends the error response our clients expect. Unfortunately, our logs contain each error twice because Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware logs the error before our error controller. How can we completely remove/replace Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware such that we (a) log the error only once and (b) completely replaces the response with our content?
We decided to use app.UseExceptionHandling('/error') over app.Use<CustomMiddlewareErrorHandler>() because changing the content in middleware always produced the "response already started" error that we never see when we use the error controller. I've found tutorials that advice testing whether the response has already started, but we always want to replace it. I suppose we need to flush the response or something like that.
Can someone point me to a practical example that (a) prevents double logging and (b) allows me to completely replace the response after an error?
In my .Net Core 2.2 application, I have a WebAPI controller that may return response codes 400 and 409.
In the development environment, I can see those codes along with the error messages. But when I deploy the application to the production environment (a virtual machine in Azure) I see just 500 errors without any details. Can anybody explain how to force IIS to return the initially sent error responds?
I return the error responds with the code like the following:
return StatusCode((int)HttpStatusCode.BadRequest, new { Message = errorMessage });
Finally, I have turned on the Failed requests tracing and it gave me a clue. The cause was in the fact that the same MIME type was configured twice, one time globally at the IIS level, and the second time at the website level. Once I removed the problematic MIME-type from the website configuration all started working correctly.
If someone have the same problem, I solved in the Error Pages area from IIS configuration. When I tried to click, I received a error with the path of web.config from another project. When I solved this error from web.config, the Error Page open correctly and everything work again.
I have a page that's doing jQuery Ajax calls to a WebMethod in one of my ASPX pages. In some cases, my WebMethod is throwing an exception with an error message. This error message is really important for the UI, because it tells what to say to the user, so he can fix his form.
This is working fine in development environnement. But, in my production Web.Config, I need to keep customErrors set to "On", so I wont show important information. Moreover, my IIS is overriding HTTP 500 errors too.
I tried the WebServices from ASMX, and it was working perfectly. IIS wasn't overriding my HTTP 500, and customErrors was not hiding the message sent by the WebService. But migrating all my ASPX's WebMethods to ASMX files is not possible for now.
I'm looking for a guideline. How my UI can gets my WebMethod's errors, while keeping my Web.Config safe for production?
You should never show a yellow screen of death to a user.
Instead, consider implementing a Response object, contaning a boolean Success, an ErrorMessage (string) and the actual result data if needed.
You can then use a JavaScript callback and check if the call succeded. If it did not, just alert() the error message.
a few days ago I start to use a SOAP service for Joomla and VirtueMart connected by a client in C#.
I tried out about a fistful functions and it works fine.
Only one function I've trouble with when I try to use it I get an error.
The developer send me a request that work for him (with my server) but I see only few differents that are not important as I think ...
Maybe somebody can help and show me impoartant differents of the requests that may be a reason for trouble?
This is the working request done with Felx (as URL as I can't insert XML:
Working Request
And this is my request that return a server fault:
Not working Request
Has somebody any idea please?
Fiddler also return an violation but I think that does not matter in this case:
Content-Length mismatch: Response Header indicated 8.958 bytes, but server send 8.959 bytes.
Also a hint how to debug may be useful.
When I send a SOAP request to my service in the IIS locally, everything works fine.
When I send a SOAP request to the same service that running on IIS on another host, everything works fine.
But when another programmer sends a SOAP request to my service, he generally gets the right response except one method in the service that returns:
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Server was unable to process request. ---> Object reference not set to an instance of an object.</faultstring>
<detail />
</soap:Fault>
I need to understand why he is receiving this error.
His SOAP request is exactly the same as SOAP Request yet mine works and his does not.
The "Object reference" error is the hardest to troubleshoot if you don't know exactly where the error is occurring. Be sure to test for null values, as the most obvious cause for this error is an object not being initialized or a variable having a null value when it's expected to contain an object or a value.
Also, be sure that you're following best practices for error handling. Try to catch the errors at the earliest possible level unless you intentionally intend to let a higher-level error handler catch the error. The fact that you're getting this error means that the error handling is not sufficient.
Finally, I'd strongly recommend including some sort of logging mechanism for your error handlers. Save the errors to the Event Log, or a database, or even email them - anything, so that you can tell exactly where the error is. Do NOT display the error back to the user.
There are several options for this. Asp.Net Health Monitoring works fine in web services as well as standard asp.net sites. There's also ELMAH, or you can write your own code to do this.
If you follow these guidelines, you won't be in a situation where you don't understand how the error is happening. You'll have the information you need to troubleshoot properly, and your apps will be more solid.
Thanks guys for all your help. I solved the problem.
The 'SoapAction' field in the soap request had the wrong URI
I recommend using a HTTP sniffer. The http sniffer will show the SOAP properties.
The "SOAPAction" property should have something like : http://tempuri.org/YourServiceMethodName