代码改变世界

asp.net 2.0 学习点滴推荐(001)

2009-05-01 20:18  kenty06  阅读(411)  评论(0编辑  收藏  举报

在asp.net中,整个文件的内容被解析为类定义,然后被编译成一个程序集,服务器短脚本块被直接加入到类定义中,分散的脚本被合并成该类的一个Render方法,该方法被调用时将所有静态和动态内容写入到响应流中,

例如:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    const int _itemcount = 10;
    string GetDisplayItem(int n)
    {
        return "Item #" + n.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple page</title>
</head>
<body>
   <h1>Test Asp.NET 2.0 Page</h1>
   <ul>
   <% for (int i = 0; i < _itemcount; i++)
      { %>
   <li>
   <%=GetDisplayItem(i)%>
   </li>
   <%} %>   
   </ul>
   <% Response.Write("<h2>Total number of items = " + _itemcount.ToString() + "</h2>"); %>
</body>
</html>

该页面生成的类代码为

public class simplepage_aspx : Page, IRequiresSessionState, IHttpHandler
{
    // Fields
    private static object __fileDependencies;
    private static bool __initialized;
    private const int _itemcount = 10;

    // Methods
    [DebuggerNonUserCode]
    public simplepage_aspx()
    {
        base.AppRelativeVirtualPath = "~/SimplePage.aspx";
        if (!__initialized)
        {
            string[] virtualFileDependencies = new string[] { "~/SimplePage.aspx" };
            __fileDependencies = base.GetWrappedFileDependencies(virtualFileDependencies);
            __initialized = true;
        }
    }

    [DebuggerNonUserCode]
    private HtmlHead __BuildControl__control2()
    {
        HtmlHead head = new HtmlHead("head");
        HtmlTitle title = this.__BuildControl__control3();
        IParserAccessor accessor = head;
        accessor.AddParsedSubObject(title);
        return head;
    }

    [DebuggerNonUserCode]
    private HtmlTitle __BuildControl__control3()
    {
        HtmlTitle title = new HtmlTitle();
        IParserAccessor accessor = title;
        accessor.AddParsedSubObject(new LiteralControl("Simple page"));
        return title;
    }

    [DebuggerNonUserCode]
    private void __BuildControlTree(simplepage_aspx __ctrl)
    {
        this.InitializeCulture();
        HtmlHead head = this.__BuildControl__control2();
        IParserAccessor accessor = __ctrl;
        accessor.AddParsedSubObject(head);
        __ctrl.SetRenderMethodDelegate(new RenderMethod(this.__Render__control1));
    }

    private void __Render__control1(HtmlTextWriter __w, Control parameterContainer)
    {
        __w.Write("\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/%22%3E/r/n/r/n");
        __w.Write("\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml/%22%3E/r/n");
        parameterContainer.Controls[0].RenderControl(__w);
        __w.Write("\r\n<body>\r\n   <h1>Test Asp.NET 2.0 Page</h1>\r\n   <ul>\r\n   ");
        for (int i = 0; i < 10; i++)
        {
            __w.Write("\r\n   <li>\r\n   ");
            __w.Write(this.GetDisplayItem(i));
            __w.Write("\r\n   </li>\r\n   ");
        }
        __w.Write("   \r\n   </ul>\r\n   ");
        int num2 = 10;
        base.Response.Write("<h2>Total number of items = " + num2.ToString() + "</h2>");
        __w.Write(base.GetType().Assembly.Location);
        __w.Write("\r\n</body>\r\n</html>\r\n");
    }

    [DebuggerNonUserCode]
    protected override void FrameworkInitialize()
    {
        base.FrameworkInitialize();
        this.__BuildControlTree(this);
        base.AddWrappedFileDependencies(__fileDependencies);
        base.Request.ValidateInput();
    }

    private string GetDisplayItem(int n)
    {
        return ("Item #" + n.ToString());
    }

    [DebuggerNonUserCode]
    public override int GetTypeHashCode()
    {
        return -328547524;
    }

    [DebuggerNonUserCode]
    public override void ProcessRequest(HttpContext context)
    {
        base.ProcessRequest(context);
    }

    // Properties
    protected HttpApplication ApplicationInstance
    {
        get
        {
            return this.Context.ApplicationInstance;
        }
    }

    protected DefaultProfile Profile
    {
        get
        {
            return (DefaultProfile) this.Context.Profile;
        }
    }
}

 

在页面任何位置增加    <% =GetType().Assembly.Location %> 来查看生成的代码文件位置以及名称