How to show a custom 404 page in ASP.NET without redirect?

.net, asp.net, asp.net-mvc, c#, vb.net

Solution

As a general ASP.NET solution, in the customErrors section in the web.config, add the redirectMode="ResponseRewrite" attribute.

<customErrors mode="On" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="/404.aspx" />
</customErrors>

Note: this internally uses a Server.Transfer() so the redirect must be an actual file on the webserver. It can't be an MVC route.

Problem

When a request is 404 in ASP.NET on IIS 7 i want a custom error page to be displayed. The URL in the address bar should not change, so no redirect. How can i do this?

Original source