• on asp.net


            终于在cnblogs上开博了,cnblogs有庞大的.net爱好者,可以交流,可以批评,这是我愿意在这里的原因,在spaces.live.com上的那一个长期无人问津。当然,我对.net现在不过刚有兴趣,属菜鸟级,不当之处或有何建议,请联系我的邮箱:j2eedesigner@163.com
             j2ee是我喜欢的东西,当然现在已经是javaee了。相对来说,比.net更喜欢。我以前曾决定不要再学.net,就像本科时候决定只学win32一样。但形势所迫,这工作岗位需要.net的还挺多。而我恰恰找不到工作。所以这四年里,我不得已又看看MFC,Java,最后还是要学习.net。
             无论什么服务器IIS,Apache,Tomcat最终要使用Http协议,无论什么FrameWork,无论stuts,webwork,还是Asp.net 2.0,最后总归要生成html文本。我不是一个可以很专注研究问题的人,所以错误是正常的。我的毅力坚持不了几分钟,我知道我喜欢手写而不喜欢打字,喜欢空想而不喜欢行动。2003年,我毕业正在打算建一个个人网站,而淘宝网也刚刚开始,到现在,淘宝已有数千万注册用户,我的网站只不过是一个网页的两行文字;2004年,p2p如日中天,几个学生正躲在宿舍里开发pplive,我也对torrent协议很感兴趣,不过我是用pp点点通来上网下载A片,到现在pplive用户也数以千万计,而我对AV也颇有认识。2005年,我筹划写我的小说已有10个年头,箫鼎此时正开始准备写《诛仙》,到现在,《诛仙》的读者也数以千万计,而我的小说依然只是一个开头而已。写技术文章不是我的强项,因为需要查阅很多东西,有时候我觉得我懂了,其实我是感觉懂了,但是写出来就不一样了,群众的眼睛是雪亮的,是我所无法糊弄的,所以我不得不打点精神,去查一些东西,当然查不到的东西只能猜测一番,给出一个自认合理的解释。我写东西一向喜欢跑题,时不时出来牢骚一番,就像莫言也喜欢在小说中跳出来,自我感慨一番一样。当然这不是好的写作方式,除非我是大家,可惜我不是,我只是找不到工作,而且找不到媳妇,尘世中的一个迷途窝囊之一。
           相对于J2ee来说,asp.net封装的东西更多,所以它就更先进,更不灵活,更没有效率,但是对开发者来说,也就更省心。好怀念那种纯粹的时候,没有中间件,自己手动连接监听回馈,多自由。这才是纯粹的程序员,这些天看linux源码,看到用汇编语言操作中断控制器建立各种系统描述符还有页表GDT/IDT表,禁不住热血沸腾,啊,这个年代的编程才是真正的编程,多么的明白直观,多么的没有效率,多么的不可移植。jeffy richer说看到了中间语言,就不再关心这些底层的东西了,实在是不解风情阿。到了j2ee以后,符合规范的服务器封装了Request和Response,并且提供了固定的流程让我们只要重载servlet的service()函数,已经不够灵活。后来的框架就更不像话了,成为了各种设计模式的试验场。其实对我们来说,没有必要做到这么松耦合。现在的上传已经不需要自己手工解析,各种框架提供了现成的函数直接可以提取出文件,真是可惜我了。
          服务器在解析jsp文件的时候实际上是生成了一个servlet并且编译了,所以它的输出就是html。.net解析一个aspx文件,实际上也是生成了一个继承自IHttphandler的类,并且编译了。我们看一看实际源代码,相信大家从源代码中得到的东西比我说的更好。有时候太追求细节往往太烦人,所以我一向崇拜那些破解者,他们的毅力与坚持实在让我汗颜。
    这是我的Test.aspx文件
    %@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

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

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:Button ID="bt" runat="server" Height="100px" OnClick="bt_Click" Text="Click Me !" />
            
    <asp:TextBox ID="TextBox1" runat="server">assd</asp:TextBox></div>
        
    </form>
    </body>
    </html
    很简单,只是两个控件。
    下面是伴随它的Test.aspx.cs文件
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;

    public partial class Test : System.Web.UI.Page
    {
        
       
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
           
    int i = 1;
        }

        
    protected void bt_Click(object sender, EventArgs e)
        
    {
            
    if ("a".Equals ((string)ViewState["Re"]))
            
    {
                bt.Height 
    = 900;
                ViewState[
    "Re"= "b";
            }

            
    else
            
    {
                bt.Height 
    = 100;
                ViewState[
    "Re"= "a";
            }

            Response.Write( 
    "ViewState :"+Request.Params["__VIEWSTATE"]+"<br/>" );
      
            
    string decodeValue = Encoding.UTF8.GetString( Convert.FromBase64String( Request.Params["__VIEWSTATE"] ) );
        
            Response.Write( 
    "ViewState decode :"+decodeValue+"<br/>" );

        }

    }

    关于ViewState是我为了测验.net关于ViewState做了些什么而加入的,大家可以不看。现在我们看看微软怎么改编组合这些东西呢。下面是微软生成的cs文件,为了方便,我们把这些放在一起。 

    public partial class Test : System.Web.SessionState.IRequiresSessionState {
            
    protected global::System.Web.UI.WebControls.Button bt;
            
    protected global::System.Web.UI.WebControls.TextBox TextBox1;
            
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;
            
    protected System.Web.Profile.DefaultProfile Profile {
            
    get {
                
    return ((System.Web.Profile.DefaultProfile)(this.Context.Profile));
            }

        }

        
        
    protected System.Web.HttpApplication ApplicationInstance {
            
    get {
                
    return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));
            }

        }

    }

    namespace ASP {
        
           
       [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
        
    public class test_aspx : global::Test, System.Web.IHttpHandler {
            
            
    private static bool @__initialized;
            
            
    private static object @__fileDependencies;
            
            
    public test_aspx() {
                
    string[] dependencies;
                
                
    #line 912304 "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\Test.aspx.cs"
                ((System.Web.UI.Page)(
    this)).AppRelativeVirtualPath = "~/Test.aspx";
                            
    if ((global::ASP.test_aspx.@__initialized == false)) {
                    dependencies 
    = new string[2];
                    dependencies[
    0= "~/Test.aspx";
                    dependencies[
    1= "~/Test.aspx.cs";
                    global::ASP.test_aspx.@__fileDependencies 
    = this.GetWrappedFileDependencies(dependencies);
                    global::ASP.test_aspx.@__initialized 
    = true;
                }

                
    this.Server.ScriptTimeout = 30000000;
            }

            
            
    private global::System.Web.UI.HtmlControls.HtmlTitle @__BuildControl__control3() {
                global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl;
                            @__ctrl 
    = new global::System.Web.UI.HtmlControls.HtmlTitle();
                
                            System.Web.UI.IParserAccessor @__parser 
    = ((System.Web.UI.IParserAccessor)(@__ctrl));
                
                            @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("无标题页"));
                
                           
    return @__ctrl;
            }

            
            
    private global::System.Web.UI.HtmlControls.HtmlHead @__BuildControl__control2() {
                global::System.Web.UI.HtmlControls.HtmlHead @__ctrl;
                            @__ctrl 
    = new global::System.Web.UI.HtmlControls.HtmlHead("head");
               global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl1;
                            @__ctrl1 
    = this.@__BuildControl__control3();
                
                            System.Web.UI.IParserAccessor @__parser 
    = ((System.Web.UI.IParserAccessor)(@__ctrl));
                            @__parser.AddParsedSubObject(@__ctrl1);
                          
    return @__ctrl;
            }

            
            
    private global::System.Web.UI.WebControls.Button @__BuildControlbt() {
                
                @__ctrl.Click 
    += new System.EventHandler(this.bt_Click);
                
    return @__ctrl;
            }

            
            
    private global::System.Web.UI.WebControls.TextBox @__BuildControlTextBox1() {
                
            }

            
            
    private global::System.Web.UI.HtmlControls.HtmlForm @__BuildControlform1() {
                global::System.Web.UI.HtmlControls.HtmlForm @__ctrl;
                
                @__ctrl 
    = new global::System.Web.UI.HtmlControls.HtmlForm();
                
                            
    this.form1 = @__ctrl;
                
                            @__ctrl.ID 
    = "form1";
                            System.Web.UI.IParserAccessor @__parser 
    = ((System.Web.UI.IParserAccessor)(@__ctrl));
                           @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("\r\n    <div>\r\n        "));
               global::System.Web.UI.WebControls.Button @__ctrl1;
                
                            @__ctrl1 
    = this.@__BuildControlbt();
                            @__parser.AddParsedSubObject(@__ctrl1);
                           @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("\r\n        "));
               global::System.Web.UI.WebControls.TextBox @__ctrl2;
                
                         @__ctrl2 
    = this.@__BuildControlTextBox1();
                                       @__parser.AddParsedSubObject(@__ctrl2);
                
              
                @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("</div>\r\n    "));
              
                
    return @__ctrl;
            }

            
            
    private void @__BuildControlTree(test_aspx @__ctrl) {
                
                
    #line 1 "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\Test.aspx"
                
    this.InitializeCulture();
                
                           System.Web.UI.IParserAccessor @__parser 
    = ((System.Web.UI.IParserAccessor)(@__ctrl));
                
                
                @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3" +
                            ".org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1" +
                            
    "999/xhtml\" >\r\n"));
               global::System.Web.UI.HtmlControls.HtmlHead @__ctrl1;
                            @__ctrl1 
    = this.@__BuildControl__control2();
                           @__parser.AddParsedSubObject(@__ctrl1);
                            @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("\r\n<body>\r\n    "));
                            global::System.Web.UI.HtmlControls.HtmlForm @__ctrl2;
                           @__ctrl2 
    = this.@__BuildControlform1();
                            @__parser.AddParsedSubObject(@__ctrl2);
                           @__parser.AddParsedSubObject(
    new System.Web.UI.LiteralControl("\r\n</body>\r\n</html>\r\n"));
                       }

            
                 
    protected override void FrameworkInitialize() {
                
    base.FrameworkInitialize();
                
    this.@__BuildControlTree(this);
                
    this.AddWrappedFileDependencies(global::ASP.test_aspx.@__fileDependencies);
                
    this.Request.ValidateInput();
            }

                   
    public override void ProcessRequest(System.Web.HttpContext context) {
                
    base.ProcessRequest(context);
            }

        }

    }

    public partial class Test : System.Web.UI.Page
    {
        
       
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
                   
    int i = 1;
        }

        
    protected void bt_Click(object sender, EventArgs e)
        
    {
            
    if ("a".Equals ((string)ViewState["Re"]))
            
    {
                bt.Height 
    = 900;
                ViewState[
    "Re"= "b";
            }

            
    else
            
    {
                bt.Height 
    = 100;
                ViewState[
    "Re"= "a";
            }

                Response.Write( 
    "ViewState :"+Request.Params["__VIEWSTATE"]+"<br/>" );
      
            
    string decodeValue = Encoding.UTF8.GetString( Convert.FromBase64String( Request.Params["__VIEWSTATE"] ) );
        
            Response.Write( 
    "ViewState decode :"+decodeValue+"<br/>" );

        }

    }

    namespace @__ASP {
        
        
        
    internal class FastObjectFactory_app_web_ndlcw7zj {
            
            
            
    #line 1 "c:\\dummy.txt"

            
    #line default
            
    #line hidden
            
            
            
    private FastObjectFactory_app_web_ndlcw7zj() {
            }

            
            
    static object Create_ASP_test_aspx() {
                
    return new ASP.test_aspx();
            }

            
            
    static object Create_ASP_default_aspx() {
                
    return new ASP.default_aspx();
            }

        }

    }

    非常庞大,我已经尽可能的去掉不需要的东西,构造button 和 textbox和label差不多,所以就把代码省略了,但button多一个事件,这行保留。有些地方解释一下,ViewState是一个StateBag对象,属于Page类的成员,所以被自然继承了,因此没有代码中没有声明它。我们可以看到实际上我们运行的是test.aspx类,它直接继承自Test和IHttphandler,我们知道,继承IHttpHandler必须要实现IsReusable属性,那代码中为什么没有呢,原来它是在Page类里。另外Page类有一个ProcessRequest方法,IHttphandler也有一个。可知aspx最后要编译成一个httphandler,然后容器调用它的processrequest方法,发现只有一行,base.proceRequest(),于是转向Test的ProcessRequest方法,也就是Page的ProcessRequest方法了,然后再这个方法里面Render各种Control,ClearViewState,调用Page_onLoad等等。这些我也不清楚,因为毕竟没找到源代码。不误己误人。
             一个小时,弄这些东西,竟然还不是很烦,有进步,错误的地方请指正,毕竟我只是初学者而已。
     

  • 相关阅读:
    [转]顶点数据压缩
    [转]将某个Qt4项目升级到Qt5遇到的问题
    「05」回归的诱惑:一文读懂线性回归
    AI漫谈:我们距离实现《庆余年》里的五竹叔机器人还有多远?
    “木兰”去哪儿了?被全国700所中小学引入的国产编程语言“木兰”,为何在官网删除了下载链接
    有哪些让人相见恨晚的Python库(一)
    2019年最值得关注的AI领域技术突破及未来展望
    为什么样本方差的分母是n-1?为什么它又叫做无偏估计?
    「04」机器学习、深度学习需要哪些数学知识?
    「03」机器学习、深度学习该怎样入门?
  • 原文地址:https://www.cnblogs.com/j2eedesigner/p/799759.html
Copyright © 2020-2023  润新知