Page not rendered when sending statuscode 500 to the client - c#

I have a page (generic handler) on which I want to return the status code 500 to the client to indicate that something is wrong. I do it like this:
Response.StatusCode = 500;
Response.StatusDescription = "Internal Server Error";
And at the same time I render a friendly message telling the user that something went wrong.
But instead of seing my message, I get the default IIS message saying something like this:
Server Error
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
And if I go in to IIS and remove the error page for 500, I get this message:
The page cannot be displayed because an internal server error has occurred.
It works as intended in IIS6, but not in IIS7. What should I do to get it working in IIS7?

You need one more line in there to bypass IIS7 taking over (based on the 500 error code you set):
Response.TrySkipIisCustomErrors = true;
Reference: HttpResponse.TrySkipIisCustomErrors

Related

RemoteServerException always throws 500 Internal Server Error?

I am trying to use RemoteServerException in my code like this:
throw new RemoteServerException(response.StatusCode,response.IsSuccessStatusCode,newUri("http://localhost:5045/myapi"),null,content);
but it always throws 500 instead of response.StatusCode that I set. why?
because it shows remote api's response in it's body like this image:

c# Rest client response error

I am trying to develop a rest client using RestSharp in C#.
Code:
var client = new RestClient("url goes here");
var response = client.Execute(new RestRequest()) as RestResponse;
Console.WriteLine(response.ResponseStatus);//Coming as Error
Console.WriteLine(response.StatusCode);//Coming as 0
I am not getting any compilation or runtime exceptions but the ResponseStatus is coming as "Error" and Status Code as "0" in the console.
can you anyone help me understand the reason for this?
Your inputs on this would be really helpful.
Thank you.
HTTP response 0 indicates that client could not connect with server and hence forth time out happened.
which means that you have a problem sending your request to the given URL, either by wrong URL or any other reason and hitting a timeout, this is why you receive status code of 0, which is not a standard HTTP status code.
for standard status code table see:
Status code table.

WebApi2 basic header authorization : Not getting Custom error message with 401 status

Not getting custom error message which i am sending. IIS is overriding my custom error with
401 - Unauthorized: Access is denied due to invalid credentials.
below is the response writing code -
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
"Invalid User");
Getting below message -
Expected result "Invalid User"
How to view my custom message. Someone help!
The "Invalid User" text that you provided to the CreateErrorResponse() method is used for the error reason. Depending on the browser that you use, usually you don't see it on the page. You have to use the developer tools in your browser to see it.
If you want to display the message on the error page, you need to put it in the page content:
return new ContentResult {
StatusCode = (int)HttpStatusCode.Unauthorized,
Content = "Invalid User"
};
However, some browsers will still not display the message, and will display their own messages associated with that code.

Return correct http error status code to browser

In my global.asax I have written code into the Application_Error event which catches all errors and shows a generic error page to the user as well as logging/emailing the error that occurred.
However, I have realised that the error page does not return the correct status code to the browser which has meant that, to a service like UptimeRobot, the site is still seen as functioning even when the page is broken.
Is it possible to derive the correct status code from an error and return it to the browser?
By default IIS intercepts the status code and sends either the debug info or a custom error page. You can deactivate this default behavior and manually set the error code through the Response object:
Context.Response.TrySkipIisCustomErrors = true;
Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Context.Response.ContentType = "text/plain";
Context.Response.Write("Your error message");

Verifying links that need authentication

I am trying to verify all the links in a particular site in C# using the request-response technique. I noticed that none of the links are returning "OK". I zeroed in on the problem. The site requires authentication. Knowing this, how should I be going about it????
Here is my code sample:
WebRequest objWebRequest;
HttpWebResponse objHttpResponse;
try
{
objWebRequest = (HttpWebRequest)WebRequest.Create(strCheckingLink);
objWebRequest.Timeout = 30000;
objHttpResponse = (HttpWebResponse)objWebRequest.GetResponse();
if (objHttpResponse.StatusCode == HttpStatusCode.Redirect)
{}
if (objHttpResponse.StatusCode == HttpStatusCode.OK)
{
httpSCode = (int)objHttpResponse.StatusCode;
httpMsg = "OK";
invalidLink = false;
}
else
{
invalidLink = true;
}
}
Do you want your verification code to correctly verify your website's pages?
In this case, you need to authenticate yourself to your website programmatically. Since the authentication is usually done via cookies, you need to obtain the authentication cookie somehow, and then attach it to every your request.
1, Timeout (MSDN) :
A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.
2,Redirect : If a page redirected to another page ( status 301 and 302), HttpWebResponse will automatic get the finally page, so status is 200 (OK)
3, If page not found (404) or Server error (500) or other, will throw WebException

Categories

Resources