aspx怎么转成java,Asp.net 2.0 中.aspx请求,即如何生成响应的Html文件(原创)

本文深入探讨了.NET Web应用程序的启动过程,从IISAPIRuntime到ISAPIRuntime的调用,再到ASP.NET 2.0中的WebDev.WebHost启动。详细解析了HttpApplication类在Http管道中的作用,包括HttpApplication.Init()方法,以及URL重写功能的实现。文章还揭示了.aspx页面处理的内部机制,如PageHandlerFactory和页面实例的动态生成。整个流程涉及到了编译、模块初始化、请求处理和输出流的生成步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于研究过内核的人肯定以为整个.net 最终开始是IISAPIRuntime.ProcessReuqest()然后调用ISAPIRuntime.ProcessRequest(IntPtr ecb, int iWRTyp)

其实还有一种方式,当你在使用.net 时,会自动在C:\WINDOWS\assembly的方件夹再次生成一个WebDev.WebHost

Microsoft.VisualStudio.WebHost.Server.Start()是整个的开始

ContractedBlock.gif

ExpandedBlockStart.gif

Code

None.gifpublicServer(intport,stringvirtualPath,stringphysicalPath,boolrequireAuthentication)

ExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gifthis.port=port;

InBlock.gifthis.vritualPath=virtualPath;

InBlock.gifthis.physicalPath=physicalPath.EndsWith(@"\", StringComparison.Ordinall)?physicalPath : (physicalPath+@"\");

InBlock.gifthis.requireAuthentication=requireAuthentication;

InBlock.gifthis.onSocketAccept=newWaitCallback(this.OnSocketAccept);//设置回调函数,asp.net 2.0流程的最终开始InBlock.gifthis.onStart=newWaitCallback(this.OnStart);

InBlock.gifthis.appManager=ApplicationManager.GetApplicationManager();

InBlock.gifthis.ObtainProcessToken();

ExpandedBlockEnd.gif    }

即,asp.net 流程开始时,会生成Server类,创建对应的Host,使用socket连接,然后

在OnSocketAccept的回调方法里会执行 host.ProcessRequest(conn);

host.ProcessRequest   --> new Request(this, conn).Process();

此处会调过HttpRuntime.ProcessRequest   (HttpWorkRequest wr) //此时wr必不是IsApiWorkRequest,而是一个SimpleWorkRequest

对ProcessRequest处理如下

HttpRuntime会先将这个请求放入RequestQueue,(队列默认有5000个请求)

如果当前线程池可用的话,立即开始一个线程处理

HttpApplication 类的一个实例在其生存期内被用于处理多个请求,但它一次只能处理一个请求,这样,成员变量才可用于存储针对每个请求的数据。

IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(extraData);

ContractedBlock.gif

ExpandedBlockStart.gif

Code

None.gifNone.gifif(applicationInstanceisIHttpAsyncHandler)

ExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gif       IHttpAsyncHandler handler2=(IHttpAsyncHandler) applicationInstance;

InBlock.gif       extraData.AsyncAppHandler=handler2;

InBlock.gif

InBlock.gif//调用HttpApplication.BeginProcessRequest开始流程

InBlock.gif//_handlerCompletionCallback为异步结束时回调函数,

InBlock.gif//extraData为HttpContextInBlock.gifhandler2.BeginProcessRequest(extraData,this._handlerCompletionCallback, extraData);

ExpandedBlockEnd.gif  }None.gifelseExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gif        applicationInstance.ProcessRequest(extraData);

InBlock.gifthis.FinishRequest(extraData.WorkerRequest, extraData,null);

ExpandedBlockEnd.gif  }None.gif

None.gif

由于Asp.net 2.0东西太多,只说说大体

在Http管道中,HttpApplication是核心

下面是HttpApplication.Init()核心代码如下

ContractedBlock.gif

ExpandedBlockStart.gif

Code

None.gifNone.gifif(applicationInstanceisIHttpAsyncHandler)

ExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gif       IHttpAsyncHandler handler2=(IHttpAsyncHandler) applicationInstance;

InBlock.gif       extraData.AsyncAppHandler=handler2;

InBlock.gif

InBlock.gif//调用HttpApplication.BeginProcessRequest开始流程

InBlock.gif//_handlerCompletionCallback为异步结束时回调函数,

InBlock.gif//extraData为HttpContextInBlock.gifhandler2.BeginProcessRequest(extraData,this._handlerCompletionCallback, extraData);

ExpandedBlockEnd.gif  }None.gifelseExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gif        applicationInstance.ProcessRequest(extraData);

InBlock.gifthis.FinishRequest(extraData.WorkerRequest, extraData,null);

ExpandedBlockEnd.gif  }None.gif

None.gif

返回用户当前是否启用Url重写,再也不用手动处理URL重写啦

UrlMappingsSection urlMappings = RuntimeConfig.GetAppConfig().UrlMappings;

flag = urlMappings.IsEnabled && (urlMappings.UrlMappings.Count > 0);

None.gif

context.ConfigurationPath

=

context.Request.ApplicationPathObject;

None.gif

using

(HttpContextWrapper wrapper

=

new

HttpContextWrapper(context))

ExpandedBlockStart.gif

ContractedBlock.gif

d1d349d5873e80d8dfba590e943d329b.png

{

InBlock.gif//初始化所有的模块InBlock.gifthis.InitModules();

InBlock.gif        

d1d349d5873e80d8dfba590e943d329b.png

InBlock.gifthis._context=context;

InBlock.gifthis._hideRequestResponse=true;

InBlock.gif//初始化自身InBlock.gifthis.Init();                                            

InBlock.gifthis._hideRequestResponse=false;                    

InBlock.gifthis._context=null;                    

InBlock.gif       ArrayList steps=newArrayList();                    

InBlock.gif//验证路径InBlock.gifsteps.Add(newValidatePathExecutionStep(this));                   

InBlock.gifif(flag)                    

ExpandedSubBlockStart.gif

ContractedSubBlock.gifd1d349d5873e80d8dfba590e943d329b.png{                        

InBlock.gif//asp.net 2.0内置URL重写功能InBlock.gifsteps.Add(newUrlMappingsExecutionStep(this));

ExpandedSubBlockEnd.gif     }InBlock.gif   steps.Add(newMapHandlerExecutionStep(this));         

InBlock.gif

d1d349d5873e80d8dfba590e943d329b.png          

InBlock.gif    steps.Add(newCallHandlerExecutionStep(this));                   

InBlock.gif 

d1d349d5873e80d8dfba590e943d329b.png 

InBlock.gif   steps.Add(newCallFilterExecutionStep(this));                   

InBlock.gif  

d1d349d5873e80d8dfba590e943d329b.png            

InBlock.gif   steps.CopyTo(this._execSteps);                    

ExpandedBlockEnd.gifthis._resumeStepsWaitCallback=newWaitCallback(this.ResumeStepsWaitCallback);

None.gif

HttpApplication.BeginProcessRequest()时会调用HttpApplication.ResumeSteps()

就是对上面IExecutionStep进行依次的处理

在MapHandlerExecutionStep时,会根据如何配置取出对应的工厂

Asp.net 2.0 根 Web.config

None.gif

<

httpHandlers

>

None.gif

<

add path

=

"

*.aspx

"

verb

=

"

*

"

type

=

"

System.Web.UI.PageHandlerFactory

"

validate

=

"

True

"

/>

None.gif

httpHandlers

>

None.gif

今天只谈.aspx

上面都是大家知道的,没什么意思啦

咱来点有意思的

搞.net 的人都知道.aspx会使用PageHandlerFactory .GetHandlerHelper()返回一个IHttpHandler

大家有没有发现

GethandlerHelper() 里有下面的方法

Page page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page), context, true, true) as Page;

ContractedBlock.gif

ExpandedBlockStart.gif

Code

None.gifNone.gifinternalstaticobjectCreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context,boolallowCrossApp,boolnoAssert)

ExpandedBlockStart.gif

ContractedBlock.gifd1d349d5873e80d8dfba590e943d329b.png{            

InBlock.gif//BuildResult            

InBlock.gif//BuildResultCompiledAssemblyBase            

InBlock.gif//BuildResultCompiledType            

InBlock.gif//BuildResult            

InBlock.gif//BuildResultNoCompileTemplateControl            

InBlock.gif//BuildResultNoCompilePageInBlock.gifITypedWebObjectFactory factory=GetVirtualPathObjectFactory(virtualPath, context, allowCrossApp, noAssert);//注意此时是什么对象,下面再谈InBlock.gifi f (factory==null)            

ExpandedSubBlockStart.gif

ContractedSubBlock.gifd1d349d5873e80d8dfba590e943d329b.png{

InBlock.gifreturnnull;            

ExpandedSubBlockEnd.gif      }InBlock.gif      Util.CheckAssignableType(requiredBaseType, factory.InstantiatedType);           

InBlock.gifusing(ClientImpersonationContext context2=newClientImpersonationContext(context))            

ExpandedSubBlockStart.gif

ContractedSubBlock.gifd1d349d5873e80d8dfba590e943d329b.png{               

InBlock.gifreturnfactory.CreateInstance();            

ExpandedSubBlockEnd.gif      }ExpandedBlockEnd.gif}None.gif

其实这个Page是一个BuildRequest,asp.net 2.0在第一次请求时会对dll再一次的编译,

举个例子你写了一个webForm1.aspx,里面有一个Button,

此时.net 会从webForm1.axp派生出一个新的类

如下

ContractedBlock.gif

ExpandedBlockStart.gif

Code

None.gifNone.gif//[CompilerGlobalScope]

None.gif//public class webform1_aspx : WebForm1, IRequiresSessionState, IHttpHandler

None.gif//{

None.gif////Fields

None.gif//private static object __fileDependencies;

None.gif//private static bool __initialized;None.gifd1d349d5873e80d8dfba590e943d329b.png

d1d349d5873e80d8dfba590e943d329b.png..

None.gif//protected override void FrameworkInitialize()

None.gif//{

None.gif//base.FrameworkInitialize();

None.gif//this.__BuildControlTree(this);//此处创建控件树,(CtronlBuilder的作用在这里!!!!!!!!!!!)

None.gif//base.AddWrappedFileDependencies(__fileDependencies);

None.gif//base.Request.ValidateInput();

None.gif//}

None.gif//public override int GetTypeHashCode()

None.gif//{

None.gif//return 0x579d6af1;

None.gif//}

None.gif//public override void ProcessRequest(HttpContext context)

None.gif//{

None.gif//base.ProcessRequest(context);

None.gif//}

None.gif//}

当您请求WebForm1时

ITypedWebObjectFactory.GetType() 是BuildResultCompiledTemplateType它是BuildResultCompiledType的实现

ITypedWebObjectFactory.CreateInstance()就是BuildResultCompiledType.CreateInstance()

它会使用

ObjectFactoryCodeDomTreeGenerator.GetFastObjectCreationDelegate(this.ResultType);

在新生成的程序集中Asp命名空间下有这个类呀,直接new webform1_aspx ()

也就是说,此时你请求webform1,实际调用的是新生所在webform1_aspx ()

下面会依次执行

CallHandlerExecutionStep   处理输出流

CallFilterExecutionStep()调用webform1_aspx.ProcessRequest()

...

下面说一下,你请求的webForm1_aspx是如何转成html的即,Render时的顺序:

原理如下图

957f5fce688bec0c9e1d7e969ad26300.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值