Programmatically retrieving ASPX Compilation Error details

G

Guest

I've Googled this for a while, to no avail. Hopefully someone can help me.
Maybe I'm using the wrong terminology.

Here's the scoop! Let's say I've got a simple *.ASPX page that has a syntax
error. For example, broken.aspx might only contain the following line of code:

<% Dim x as %>

When I visit this page from my Web browser, I see all the helpful ASP.NET
Server Error information, including:

Compilation Error
Compiler Error Message: BC30182: Type expected.
Source Error: Line 1: <% Dim x as %>
Source File: c:\inetpub\mysite\broken.aspx

This is great information; exactly what I need to troubleshoot the problem!
However on my site, I trap the Exception in my Global.asax.vb's
Application_Error handler so that I'm notified when an error occurs. Usually
I'm able to cull useful information from the Exception, but in the event of a
Compilation Error, here's all I can see:

System.Web.HttpException: External component has thrown an exception.
---> System.Web.HttpCompileException: External component has thrown an
exception.
at
System.Web.Compilation.BaseCompiler.ThrowIfCompilerErrors(CompilerResults
results, CodeDomProvider codeProvider, CodeCompileUnit sourceData, String
sourceFile, String sourceString)
at System.Web.Compilation.BaseCompiler.GetCompiledType()
at System.Web.UI.PageParser.CompileIntoType()
at System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation()
--- End of inner exception stack trace ---
at System.Web.UI.TemplateParser.GetParserCacheItemInternal(Boolean
fCreateIfNotFound)
at System.Web.UI.TemplateParser.GetParserCacheItemWithNewConfigPath()
at System.Web.UI.TemplateParser.GetParserCacheItem()
at
System.Web.UI.TemplateControlParser.CompileAndGetParserCacheItem(String
virtualPath, String inputFile, HttpContext context)
at System.Web.UI.TemplateControlParser.GetCompiledInstance(String
virtualPath, String inputFile, HttpContext context)
at System.Web.UI.PageParser.GetCompiledPageInstanceInternal(String
virtualPath, String inputFile, HttpContext context)
at System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context,
String requestType, String url, String path)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String
requestType, String path, String pathTranslated, Boolean useAppConfig)
at
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)

The above is the result of "Server.GetLastError().ToString()" in my
Application_Error code.

So now I'm wondering, how can I *programmatically* learn the same
information shown in the first example output, when Application_Error event
handler executes?

I'm using the .NET Framework 1.1 SP 1.

Thanks for your help!
 
D

David Browne

Plat said:
I've Googled this for a while, to no avail. Hopefully someone can help me.
Maybe I'm using the wrong terminology.

Here's the scoop! Let's say I've got a simple *.ASPX page that has a
syntax
error. For example, broken.aspx might only contain the following line of
code:

<% Dim x as %>

At the risk of being unhelpful...

Inline server-side script is allowed for backwards-compatability with asp,
but it should be avoided. This is just one of the many problems you will
have using inline VB in aspx.

Move all of your server-side code to code-behind classes and you will catch
compile errors at compile-time, where they belong. As well as better type
safety, easier debugging, intellisense, etc.

David
 
N

Nicole Calinoiu

If you cast the Exception to an HttpCompileException (assuming the cast is
possible <g>), it should be possible to read the error details (including
line numbers) from the Errors collection of the CompilerResults object
exposed via the exception's Results property.

HTH,
Nicole
 
G

Guest

Very cool. You're my hero! :)

I was able to cast Server.GetLastError().GetBaseException() to a
System.Web.HttpCompileException in these cases, and pull the error details
from it, as you suggested (MSDN's doc examples were very helpful here).
Thanks for the help!

Out of curiosity, what's the reason why we see the "External component has
thrown an exception" error by default instead of something more detailed?

Thanks again.
 
N

Nicole Calinoiu

System.Web.HttpCompileException extends System.Web.HttpException, which
extends System.Runtime.InteropServices.ExternalException. Neither
HttpCompileException nor HttpException set their Message property. The only
constructor for HttpCompileException runs through the default constructors
for the two base types mentioned above, so the first time a Message setter
is hit is the default constructor for ExternalException, which sets it to
the text you see in the exception details.

HTH,
Nicole
 
P

pathurin

Just incase:
I found some help in this reference:

http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#32899f56c254350f

*************************Here is what it
says*************************************

It's hard to know but your Stack Trace implies that something goes
wrong
in the dynamic process of creating assembly that represent the page
HTML
definition. What I suggest:


1) Delete the cache of your application from asp.net temporary files


directory located under C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
2) Double check with source safe for changes between the last deployed
version and the current one in the pages HTML.
HTH


Natty Gur[MVP]


blog : http://weblogs.asp.net/ngur

Thanks.
 
G

Guest

HttpCompileException ex = (HttpCompileException)
ctx.Server.GetLastError().GetBaseException();
string txtError = ex.GetHtmlErrorMessage().ToString();

vb net
Dim exh As HttpCompileException = Server.GetLastError.GetBaseException()
Dim txtError As String = exh.GetHtmlErrorMessage().ToString()

"Plat" escribió:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top