你們都知道,在開始WebForm程序時,一個WebForm由.cs代碼文件與.aspx頁面文件組成。在aspx文件中能夠嵌入C#代碼,但沒法在aspx的嵌入C#代碼中定義類,函數和字段等。這樣,就限制了單aspx文件場景時所能發揮的功能了。web
下面咱們就來破除這個限制:app
首先來看看下面的Default.aspx文件:ide
<%@ Page Language="C#" AutoEventWireup="true" %> <% //=============== //字符串拼接開始 //=============== System.Diagnostics.Debug.Print("Hello"); } //字段定義 public const String ACTION_KEY = "action"; //類定義 public class Class1 { public String Name; //=============== //字符串拼接結束 //=============== %>
表面上看起來,下面的Class1類沒有封閉,應該會致使編譯錯誤,但編譯一下試試看呢。結果倒是編譯成功。這樣就能夠自定義類和字段了。函數
原理就是aspx文件在編譯的時候會動態生成代碼,用<% %>包含的部分就會被拼接到相似於@__Render__control1方法的內部,這種作法那麼下面的作法就有點相似於SQL注入了。根據上面的aspx文件,會動態生成以下的代碼文件:工具
#pragma checksum "E:\工做項目\loncomip\DCIMSClient_1.2\DCIMSWebSite\Default.aspx" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6F470031E504F5735FDF857C2F72979997EAFC5C" //------------------------------------------------------------------------------ // <auto-generated> // 此代碼由工具生成。 // 運行時版本:4.0.30319.17929 // // 對此文件的更改可能會致使不正確的行爲,而且若是 // 從新生成代碼,這些更改將會丟失。 // </auto-generated> //------------------------------------------------------------------------------ namespace ASP { #line 285 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.Profile; #line default #line hidden #line 280 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Text.RegularExpressions; #line default #line hidden #line 282 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.Caching; #line default #line hidden #line 278 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Configuration; #line default #line hidden #line 284 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.Security; #line default #line hidden #line 289 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.UI.HtmlControls; #line default #line hidden #line 287 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.UI.WebControls; #line default #line hidden #line 276 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Collections; #line default #line hidden #line 275 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System; #line default #line hidden #line 286 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.UI; #line default #line hidden #line 281 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web; #line default #line hidden #line 283 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.SessionState; #line default #line hidden #line 277 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Collections.Specialized; #line default #line hidden #line 279 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Text; #line default #line hidden #line 288 "C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\web.config" using System.Web.UI.WebControls.WebParts; #line default #line hidden [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()] public class default_aspx : global::System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler { private static bool @__initialized; private static object @__fileDependencies; [System.Diagnostics.DebuggerNonUserCodeAttribute()] public default_aspx() { string[] dependencies; ((global::System.Web.UI.Page)(this)).AppRelativeVirtualPath = "~/Default.aspx"; if ((global::ASP.default_aspx.@__initialized == false)) { dependencies = new string[1]; dependencies[0] = "~/Default.aspx"; global::ASP.default_aspx.@__fileDependencies = this.GetWrappedFileDependencies(dependencies); global::ASP.default_aspx.@__initialized = true; } this.Server.ScriptTimeout = 30000000; } protected System.Web.Profile.DefaultProfile Profile { get { return ((System.Web.Profile.DefaultProfile)(this.Context.Profile)); } } protected override bool SupportAutoEvents { get { return false; } } protected System.Web.HttpApplication ApplicationInstance { get { return ((System.Web.HttpApplication)(this.Context.ApplicationInstance)); } } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private void @__BuildControlTree(default_aspx @__ctrl) { #line 1 "E:\工做項目\loncomip\DCIMSClient_1.2\DCIMSWebSite\Default.aspx" this.InitializeCulture(); #line default #line hidden @__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(this.@__Render__control1)); } private void @__Render__control1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) { #line 2 "E:\工做項目\loncomip\DCIMSClient_1.2\DCIMSWebSite\Default.aspx" //=============== //字符串拼接開始 //=============== System.Diagnostics.Debug.Print("Hello"); } //字段定義 public const String ACTION_KEY = "action"; //類定義 public class Class1 { public String Name; //=============== //字符串拼接結束 //=============== #line default #line hidden } [System.Diagnostics.DebuggerNonUserCodeAttribute()] protected override void FrameworkInitialize() { base.FrameworkInitialize(); this.@__BuildControlTree(this); this.AddWrappedFileDependencies(global::ASP.default_aspx.@__fileDependencies); this.Request.ValidateInput(); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public override int GetTypeHashCode() { return 5381; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public override void ProcessRequest(System.Web.HttpContext context) { base.ProcessRequest(context); } } }
但願能對你們有用,哈哈。ui