This is how you configure a custom 404 error page for both ASP.NET and non-ASP.NET requests:
<configuration>
<system.web>
<compilation targetFramework="4.0" />
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="http404.aspx" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/http404.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
</configuration>
As others already pointed out, you should not use an HTTP redirection to send the user to the home page, this is not only confusing to users but also to machines (e.g. search engines). It is important to use the 404 status code and not a 3xx code.
You can achieve the desired functionality using meta refresh on HTML:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Not Found</title>
<meta http-equiv="refresh" content="5;url=/"/>
</head>
<body>
<h1>Not Found</h1>
<p>Redirecting to Home...</p>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…