You've probably noticed that when you debug an error which comes from an ASPX or ASCX file (literally, not from a corresponding code-behind file), ASP.NET displays an error page showing you the source file and the line on which the error occurs. The source file being displayed is automatically generated from parsing the page/control. The question is: how can I see this source file without purposely causing an exception?
I'd love to hear that there is some programmatic way (the complexity doesn't matter) to generate source files (preferrably, .cs
) from a series of ASPX/ASCX files.
Example. Consider the following ASPX page (the code-behind file may even be absent):
<%@ Page Language="C#"%>
<%@ Register Assembly="AspxGen" Namespace="AspxGen" TagPrefix="ag" %>
<html>
<head><title>Some Title</title></head>
<body>
<form id="form1" runat="server">
<asp:Label runat="server" Text="<%# ThereIsNoSuchProperty %>" />
</form>
</body>
</html>
When I open the page in the browser, the following error is shown:ASPX Compiler Error http://www.ko-sw.com/misc/sc_error_aspx.png
When I click the highlighted link, it shows the command issued to the C# compiler:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE>
"c:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe" /t:library /utf8output
/R: REFERENCES GO HERE
/debug- /optimize+ /w:4 /nowarn:1659;1699;1701 /warnaserror-
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f66a2574\25dd72a2\App_Web_sqj3krv3.0.cs"
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f66a2574\25dd72a2\App_Web_sqj3krv3.1.cs"
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
d:\Dev\AspxGen\AspxGen\Default.aspx(9,80): error CS0103: The name 'ThereIsNoSuchProperty' does not exist in the current context
This means, theoretically I can open the .CS files passed to the compiler (namely, App_Web_sqj3krv3.0.cs
and App_Web_sqj3krv3.1.cs
) and see what ASP.NET has generated from my ASPX markup. Trying to rephrase the previous question: how can I obtain this file from an arbitrary ASPX file (assuming that the file is correct and hence no info will be given on where to search)?
Any tips on that?
You can find the generated code within the following directory:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
To determine the exact path, use following code snippet.
System.Web.HttpRuntime.CodegenDir
ASP.NET uses BuildProvider and CodeDOM
to generate code for WebForms/UserControls. You can create a custom BuildProvider to generate custom code for particular extension. Take a look at following article for more information.