views:

413

answers:

3

Hi everyone,

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

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?

A: 

Are you using visual studio to write your code? If so, you can write the code for the aspx pages using the code behind feature, which generates a .cs file.

chama
Looks like I'm a bit unclear in the question. I'll try to rephrase that.
Kerido
Just reread the question - are you writing your asp code with inline c#?
chama
No, I use Visual Studio to write both ASPX and ASPX.CS files. I only put markup into the ASPX and place all the logic in the business layer or a corresponding code-behind file. All I need is accessing the file generated from an ASPX page when the page is parsed.
Kerido
A: 

ASP.Net pages, user controls and the likes are 'compiled on request'. The error you seeing is probably about a 'compilation failure' instead of a runtime failure.

You can use the aspnet_compiler.exe to 'precompile' your website. This would yield you 'placeholder' .aspx files and assemblies containing your compiled classes.

using this tool you will be able to determine if the compilation is failing (it will very likely fail pre-compilation using the aspnet_compiler.exe as well). Otherwise you are left with pure assemblies containing classes which can be decompiled using tools like "Reflector".

Hope this helps,

Marvin Smit
I edited the question to better explain what I need. The thing you're talking about is from a different subject.
Kerido
+1  A: 

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.

http://aspalliance.com/1102

Mehdi Golchin