MSajjadi
MSajjadi

Reputation: 3839

404 error in C#

How can I handle manually the 404 error in C#?

I want to check some conditions in 404 error and then redirect it to the proper page.

I know the web.config settings, but you can't check conditions in web.config file, can you?

Upvotes: 1

Views: 4448

Answers (2)

talha2k
talha2k

Reputation: 25493

Use the HttpStatusCode enumeration, specifically HttpStatusCode.NotFound:

Something like:

WebException we;
HttpWebResponse errorResponse = (HttpWebResponse)we.Response;
if (errorResponse.StatusCode == HttpStatusCode.NotFound)
{
    //
}

Reference: How can I catch a 404?

Similar questions:

Upvotes: 5

MSajjadi
MSajjadi

Reputation: 3839

I found a solution myself:

var sr = Server.GetLastError() as HttpException;
if (sr.GetHttpCode() == 404)
    ...

Upvotes: 1

Related Questions