As your stack trace shows and you observed in Application_BeginRequest
not getting called, this error occurs in IIS's request handling before your application code even gets called - the answer to your first question: therefore, your Web.config
and Application_Error
implementation are not considered.
A related discussion on ASP.NET forums indicates as much too.
On my local IIS (7.5) server, I get back...
Bad Request - Invalid URL
HTTP Error 400. The request URL is invalid.
...for the URL http://localhost/%&what
.
Curious, I tried setting a static custom error page for HTTP status code 400 under my local server's Error Pages in IIS Manager, also configuring my local server to use custom error pages for local and remote requests; but I never saw the custom error page I set for this particular error.
A TechNet article explains why:
You cannot customize the following HTTP errors: 400, 403.9, 411, 414, 500, 500.11, 500.14, 500.15, 501, 503, and 505.
So I think the answer to your second question is, no, IIS does not provide a way for you to display a custom error page for this.