Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5953675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:51:37+00:00 2026-05-22T17:51:37+00:00

I want to be able to generate IIS’s standard 404 response, not a custom

  • 0

I want to be able to generate IIS’s standard 404 response, not a custom 404 page, in response to an error condition in a code-behind class. My Page_Load method looks like this:

protected void Page_Load(object sender, System.EventArgs e)
{
    this.a = ...
    ...
    if (this.a == null) ... // *** generate a 404
}

So if a is not null the .aspx file renders, etc, but if a is null I want the standard “The resource cannot be found” page to be shown with a 404 response code.

If I use:

if (a == null) Response.StatusCode = 404;

(which is what seems to be the correct method according to what I can find) the page continues to render (and gives a NullReferenceException with a response code of 500 when it tries to use a).

If I use:

if (a == null) throw new HttpException(404, "Not found");

The response code is 404 but the page content is ASP.NET’s unhandled exception page showing the HttpException (which is then shown as the generic “Runtime Error” when customErrors is On).

I want to show IIS’s 404 page because the users will understand that, but they probably won’t check the response code of the page if it shows a server error.

EDIT: It looks like I can’t do this exactly. Given @Smudge202’s answer I let my code-behind throw the HttpException and added code to Global.Application_Error() to handle those exceptions:

void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();

    if (exception is HttpException)
    {
        var httpException = (HttpException)exception;
        if (httpException.GetHttpCode() == 404)
        {
            var context = HttpContext.Current;
            context.Response.Clear();
            context.Response.StatusCode = 404;
            context.Response.StatusDescription = "Not Found";
            context.Response.Write("<h1>404 Not Found</h1>");
            context.Server.ClearError();
        }
    }
    // ...
}

This lets me set my own response to the exception. This approach has two drawbacks:

  • What I really wanted to do was revert control back to IIS and let it display its default 404 response, I can’t find a way to do that
  • The new response (written by context.Response.Write()) is shown for every 404, not just ones generated in code

So it looks like I will need to either:

  • From the code-behind, redirect to a bespoke page explaining that the ‘thing’ can’t be found (which also sets the 404 response code), or
  • Set up a custom global 404 page
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:51:37+00:00Added an answer on May 22, 2026 at 5:51 pm

    I doubt this is a clean solution, but it might work…

    If you combine this post (and several similar I’m sure). It seems from global asax writing a 404 out to the response stream bypasses the custom errors.

    With that in mind, you could intentionally raise a custom exception in your code behind. Check for that custom exception in Application_Error, and respond with 404 from there? That will prevent the page from rendering, and everything should work as you’ve asked?

    EDIT:

    If this is shown for every 404 exception and you don’t want it to be, create a custom exception for your original code behind to fire. Only catch that particular exception in Application_Error, allow the others to pass through as normal?

    I also found this thread where the OP was trying, like you, to raise the actual 404 page, and not a mock-up of it. There are a couple suggestions on there on how to do it, no idea if any of them work, but probably worth trying quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to generate C code dynamically and re-load it quickly
I have an ASP.NET page that I want to be able to generate and
to export some data i want to be able to generate an html output.
I want to be able to replicate only the folder structure (not the contents)
I want to be able to generate random strings from the windows command prompt.
I want to be able to generate help documentation from existing topic files (.AML)
Is there some source code pretty printer out there that is able to generate
I want to be able to generate a DataContract from a XSD file, preferably
I want to write a c code to generate tone using array output to
I want to be able to generate a complete XML file, given a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.