0

I wrote an simple Web Service in ASP.NET-MVC5 framework. The problem I am faced with is custom status code and response message.

I've tried a lot to find answer here, but solution, that i've found here, didn't work for me.

I use standard try/catch approach to handle my errors. When the catch block recieves an error, it sends a response to the client with a custom error code and JSON file with some additional information about the error/exception.

When I run my application on IIS express it works fine when I send requests from the same server. The same situation presents when I run an application on IIS 7.5 on dev environment and on IIS 8.5 on production environment. But the problem occurs when client try to send request to my application and in case of an error inside the application code, the client receives just the default error page with 400 status code.

I've tried to set CustomError to different modes, also tried different combination with HttpErrors node etc.
Doesn't work for me. If CustomError mode set is to ON it stopped working for me at all - even for local requests. That's how I send response to Client:

try

{ } catch () {

Response.StatusCode = 406;

StringBuilder errorMessage = new StringBuilder();

//here I generate json file to be sent with response

string exception = errorMessage.ToString();

return Json(exception, JsonRequestBehavior.AllowGet);

}

Is there any way to make visible response messages for clients' applications not using custom error pages and redirection to them?

P.s. In SO I found a lot of similar question, but there wasn't a direct answer for my question. The main difference between my question and questions i found here, were custom error pages. But for me was anough just send json in response in case of error/exception and make it visible in response content in client side.

4the3eam
  • 194
  • 1
  • 12
  • Are you saying that, when there are no errors during execution, the client is getting the JSON response as expected? Or is it that, the client always gets a 400 - Bad Request error. Because in that case, it has something to do with [CORS](http://stackoverflow.com/questions/7001846/how-to-handle-options-method-in-asp-net-mvc) – Oguz Ozgul Nov 09 '15 at 09:42
  • @CORS – Oguz Ozgul No, i did not mean this. I sent bad request for my applciation and wanted to test my error handling. For example there is property IsActive which can accept 0 or 1 values. And in case when client send another value (e.g 4) to this property i sent a response with custom status code and message that value is wrong. But when i send request to my application from the server where this api is installed, this message is shown up, but when any other client application send request to the same server, it just receives standard bad request without any message. – 4the3eam Nov 09 '15 at 10:15
  • Can you add the code block where you set the response status code and content? – Oguz Ozgul Nov 09 '15 at 10:23
  • I've found it I think.. [Look here](https://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v=vs.118).aspx#P:System.Web.Http.HttpConfiguration.IncludeErrorDetailPolicy). You should set this to "Always" – Oguz Ozgul Nov 09 '15 at 10:31
  • @Oguz Ozgul I added code block with way i send response to clients. – 4the3eam Nov 09 '15 at 10:47
  • Did you try setting the IncludeErrorDetail to Always? – Oguz Ozgul Nov 09 '15 at 11:03
  • @OguzOzgul Yes, i tried, but when `IncludeErrorDetailPolicy` is set on Always, it returns additional information about an error/exception. And it didn't solve my problem, because i want sent back the json file, where i provide additional information with parameters that are important for clients' applications. – 4the3eam Nov 09 '15 at 11:18

1 Answers1

0

I think your problem is different than you are thinking ... I faced the problem previously ... In my case it was related to IIS re-register with asp.net ...

try below process

in command prompt

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -i

Be sure to run the command prompt as administrator

If still not solved try to find answer in below thread

Asp.net 4.0 has not been registered

Community
  • 1
  • 1
Moumit
  • 8,314
  • 9
  • 55
  • 59
  • I registered .net 4 after i installed it on servers. Before that my application didn't work at all because of missing appropriate version of framework installed on severs. – 4the3eam Nov 09 '15 at 10:58