访问从 ASPX/ASCX 文件生成的源代码
您可能已经注意到,当您调试来自 ASPX 或 ASCX 文件(字面意思是,不是来自相应的代码隐藏文件)的错误时,ASP.NET 会显示一个错误页面,向您显示源文件以及发生错误的行。发生错误。显示的源文件是通过解析页面/控件自动生成的。问题是:如何在不故意引起异常的情况下查看此源文件?
我很想听听有某种编程方式(复杂性并不重要)来生成源文件(最好是, .cs
) 来自一系列 ASPX/ASCX 文件。
示例。考虑以下 ASPX 页面(代码隐藏文件甚至可能不存在):
<%@ 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>
当我在浏览器中打开该页面时,显示以下错误:ASPX 编译器错误 http://www.ko-sw.com/misc/sc_error_aspx.png
当我单击突出显示的链接时,它显示了向C# 编译器:
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
这意味着,理论上我可以打开传递给编译器的 .CS 文件(即 App_Web_sqj3krv3.0.cs
和 App_Web_sqj3krv3.1.cs
),看看会发生什么ASP.NET 已从我的 ASPX 标记生成。尝试改写上一个问题:如何从任意 ASPX 文件获取此文件(假设该文件是正确的,因此不会给出有关搜索位置的信息)?
对此有什么提示吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在以下目录中找到生成的代码:
要确定确切的路径,请使用以下代码片段。
ASP.NET 使用 BuildProvider 和
CodeDOM
为 WebForms/UserControls 生成代码。您可以创建自定义 BuildProvider 来为特定扩展生成自定义代码。请查看以下文章以获取更多信息。http://aspalliance.com/1102
You can find the generated code within the following directory:
To determine the exact path, use following code snippet.
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
您使用 Visual Studio 编写代码吗?如果是这样,您可以使用代码隐藏功能编写 aspx 页面的代码,该功能会生成 .cs 文件。
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.
ASP.Net 页面、用户控件等都是“根据请求编译的”。您看到的错误可能是关于“编译失败”而不是运行时失败。
您可以使用 aspnet_compiler.exe 来“预编译”您的网站。这将生成包含已编译类的“占位符”.aspx 文件和程序集。
使用此工具,您将能够确定编译是否失败(使用 aspnet_compiler.exe 预编译也很可能失败)。否则,您将留下包含可以使用“Reflector”等工具反编译的类的纯程序集。
希望这有帮助,
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,