ASP论文外文翻译---从底层了解ASP.NET的结构_第1页
ASP论文外文翻译---从底层了解ASP.NET的结构_第2页
ASP论文外文翻译---从底层了解ASP.NET的结构_第3页
ASP论文外文翻译---从底层了解ASP.NET的结构_第4页
ASP论文外文翻译---从底层了解ASP.NET的结构_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、原文1A low-level Look at the ASP.NET ArchitectureAbstractASP.NET is a powerful platform for building Web applications that provides a tremendous amount of flexibility and power for building just about any kind of Web application. Most people are familiar only with the high level frameworks like WebFor

2、ms and WebServices which sit at the very top level of the ASP.NET hierarchy. In this article Ill describe the lower level aspects of ASP.NET and explain how requests move from Web Server to the ASP.NET runtime and then through the ASP.NET Http Pipeline to process requests. What is ASP.NETLets start

3、with a simple definition: What is ASP.NET? I like to define ASP.NET as follows:ASP.NET is a sophisticated engine using Managed Code for front to back processing of Web Requests.It's much more than just WebForms and Web ServicesASP.NET is a request processing engine. It takes an incoming request

4、and passes it through its internal pipeline to an end point where you as a developer can attach code to process that request. This engine is actually completely separated from HTTP or the Web Server. In fact, the HTTP Runtime is a component that you can host in your own applications outside of IIS o

5、r any server side application altogether. The runtime provides a complex yet very elegant mechanism for routing requests through this pipeline. There are a number of interrelated objects, most of which are extensible either via subclassing or through event interfaces at almost every level of the pro

6、cess, so the framework is highly extensible. Through this mechanism its possible to hook into very low level interfaces such as the caching, authentication and authorization. You can even filter content by pre or post processing requests or simply route incoming requests that match a specific signat

7、ure directly to your code or another URL. There are a lot of different ways to accomplish the same thing, but all of the approaches are straightforward to implement, yet provide flexibility in finding the best match for performance and ease of development.The entire ASP.NET engine was completely bui

8、lt in managed code and all of the extensibility functionality is provided via managed code extensions. This is a testament to the power of the .NET framework in its ability to build sophisticated and very performance oriented architectures. Above all though, the most impressive part of ASP.NET is th

9、e thoughtful design that makes the architecture easy to work with, yet provides hooks into just about any part of the request processing.With ASP.NET you can perform tasks that previously were the domain of ISAPI extensions and filters on IIS with some limitations, but its a lot closer than say ASP

10、was. ISAPI is a low level Win32 style API that had a very meager interface and was very difficult to work for sophisticated applications. Since ISAPI is very low level it also is very fast, but fairly unmanageable for application level development. So, ISAPI has been mainly relegated for some time t

11、o providing bridge interfaces to other application or platforms. But ISAPI isnt dead by any means. In fact, ASP.NET on Microsoft platforms interfaces with IIS through an ISAPI extension that hosts .NET and through it the ASP.NET runtime. ISAPI provides the core interface from the Web Server and ASP.

12、NET uses the unmanaged ISAPI code to retrieve input and send output back to the client. The content that ISAPI provides is available via common objects like HttpRequest and HttpResponse that expose the unmanaged data as managed objects with a nice and accessible interface.The ISAPI ConnectionISAPI i

13、s a low level unmanged Win32 API. The interfaces defined by the ISAPI spec are very simplistic and optimized for performance. They are very low level dealing with raw pointers and function pointer tables for callbacks - but they provide he lowest and most performance oriented interface that develope

14、rs and tool vendors can use to hook into IIS. Because ISAPI is very low level its not well suited for building application level code, and ISAPI tends to be used primarily as a bridge interface to provide Application Server type functionality to higher level tools. For example, ASP and ASP.NET both

15、are layered on top of ISAPI as is Cold Fusion, most Perl, PHP and JSP implementations running on IIS as well as many third party solutions such as my own Web Connection framework for Visual FoxPro. ISAPI is an excellent tool to provide the high performance plumbing interface to higher level applicat

16、ions, which can then abstract the information that ISAPI provides. In ASP and ASP.NET, the engines abstract the information provided by the ISAPI interface in the form of objects like Request and Response that read their content out of the ISAPI request information. Think of ISAPI as the plumbing. F

17、or ASP.NET the ISAPI dll is very lean and acts merely as a routing mechanism to pipe the inbound request into the ASP.NET runtime. All the heavy lifting and processing, and even the request thread management happens inside of the ASP.NET engine and your code.As a protocol ISAPI supports both ISAPI e

18、xtensions and ISAPI Filters. Extensions are a request handling interface and provide the logic to handle input and output with the Web Server its essentially a transaction interface. ASP and ASP.NET are implemented as ISAPI extensions. ISAPI filters are hook interfaces that allow the ability to look

19、 at EVERY request that comes into IIS and to modify the content or change the behavior of functionalities like Authentication. Incidentally ASP.NET maps ISAPI-like functionality via two concepts: Http Handlers (extensions) and Http Modules (filters). Well look at these later in more detail. ISAPI is

20、 the initial code point that marks the beginning of an ASP.NET request. ASP.NET maps various extensions to its ISAPI extension which lives in the .NET Framework directory:本文摘自http:/www.west-译文1从底层了解ASP.NET的结构· 摘要ASP.NET是一个用于构建Web程序的强大平台,提供了强大的柔性和能力以至于它可以构建任意的Web程序。许多人仅仅对处于ASP.NET高层次的框架如:WebForm

21、s和WebServices比较熟悉,因此,在这篇文章里,我将会阐述有关ASP.NET比较底层的知识,并且将会解释,如何将请求从Web Server移交给ASP.NET运行时,然后通过ASP.NET HTTP管道处理这些请求。1 ASP.NET是什么?我们从最简单的定义开始,ASP.NET是什么?我通常喜欢用如下语句来描述ASP.NET。ASP.NET是完全使用托管代码处理Web请求的一个成熟引擎平台。它不仅仅只是WebForms和WebServices。 ASP.NET是一个请求处理引擎。它获取客户端请求,然后通过它内置的管道,把请求传到一个终点,在这个终点,开发者可以添加处理这个请

22、求的逻辑代码。实际上这个引擎和HTTP或者Web Server是完全分开的。事实上,HTTP运行时是一个组件,你可以把它宿主在IIS之外的应用程序上。甚至完全可以和其它的服务组合在一起。通过使用内置的管道路由请求,HTTP运行时提供了一套复杂的,但却很优雅的机制。在处理请求的每一个层面都牵涉到许多对象,但大多数对象都可以通过派生或者事件接口来扩展。所以,此框架具有非常高的可扩展性。通过这一套机制,可以进入较低层次的接口如:缓存,身份验证,授权等是有可能的。你可以在处理请求之前或之后过滤内容,或者仅仅把匹配指定签名的客户端请求直接路由到你的代码里或转向其它的URL。针对同一件事情,可以通过不同的

23、处理方法完成,而且实现代码都非常的直观。除此之外,在容易开发和性能之间,HTTP运行时还提供了最佳的灵活性。 整个ASP.NET引擎完全构建在托管代码里,所有的扩展性功能都是通过托管代码的扩展提供。对于功能强大的.NET框架而言,使用自己的东西,构建一个成熟的、高性能的引擎体系结构已经成为一个遗嘱。尽管如此,但重要的是,ASP.NET给人印象最深的是高瞻远瞩的设计,这使得在其之上的工作变得非常容易,并且提供了几乎可以钩住请求处理当中任意部分的能力。 使用ASP.NET可以完成一些任务,之前这些任务是使用IIS上的ISAPI扩展和过滤来完成的。尽管还有一些限制,但与ASP相比

24、,已经有了很大的进步。ISAPI是底层Win32样式的API,仅它的接口就有1兆,这对于大型的程序开发是非常困难的。由于ISAPI是底层的接口,因此它的速度也是非常的快。但对于企业级的程序开发是相当的难于管理的。所以,在一定的时间内,ISAPI主要充当其它应用程序或平台的桥接口。但是无论如何,ISAPI没有被废弃。事实上,微软平台上的ASP.NET和IIS的接口是通过宿主在.NET里的ISAPI扩展来通信的,然后直达ASP.NET运行时。ISAPI提供了与Web Server通信的核心接口,然后ASP.NET使用非托管代码获取请求以及对客户端请求发出响应。ISAPI提供的内容经由公共对象类似于

25、HttpRequest和HttpResponse,通过一个设计优良的、可访问的接口,以托管对象的方式暴露非托管数据。2 从浏览器到ASP.NET让我们从一个典型的ASP.NET Web请求的生命周期的起点开始。用户通过在浏览器中键入一个URL,点击一个超链接,提交一个HTML表单(一个post请求),或者一个客户端程序调用基于ASP.NET的WebService(通过ASP.NET提供服务)。在服务器端,IIS5或者IIS6将会收到这个请求。ASP.NET的底层通过ISAPI扩展与IIS通信,然后,通过ASP.NET,这个请求通常被路由到一个带有.aspx扩展名的页面。但是,这个处理过程如何工

26、作,则完全依赖于HTTP处理器(handler)的执行。这个处理器将被安装用于处理指定的扩展。在IIS中,.aspx经由“应用程序扩展”被映射到ASP.NET ISAPI的dll文件:aspnet_isapi.dll。每一个触发ASP.NET的请求,都必须经由一个已经注册的,并且指向aspnet_isapi.dll的扩展名来标识。注:ISAPI是自定义Web请求处理中第一个并且具有最高性能的IIS入口点。依靠扩展名,ASP.NET把一个请求路由到一个恰当的处理器,该处理器则负责处理这个请求。举个例子,WebServices的扩展名.asmx不会把一个请求路由到磁盘上的某一个页面,而是会路由到在

27、定义中附加了指定特性(WebMethodAttribute)的类,此特性会把它标识成一个Web Services的实现。许多其它的处理器将随着ASP.NET一起被安装。当然也可以定义你自己的处理器。在IIS里所有的HttpHandler被映射并指向ASP.NET ISAPI扩展,并且这些HttpHandler也都在web.config里配置,用于把请求路由到指定的HTTP处理器里执行。每一个处理器都是一个.NET类,用于处理指定的扩展。而这些处理器可以处理简单到只有几行代码的Hello World,也可以处理复杂到类似ASP.NET的页面以及执行WebService。就目前而言,仅仅需要理解扩

28、展就是一种基本的映射机制,ASP.NET用它可以从ISAPI里获取一个请求,然后把请求路由到指定处理该请求的处理器中。原文2 Thirteen ASP.NET MVC extensibility points you have to knowAbstractOne of the main design principles ASP.NET MVC has been designed with is extensibility. Everything (or most of) in the processing pipeline is replaceable so, if you dont li

29、ke the conventions (or lack of them) that ASP.NET MVC uses, you can create your own services to support your conventions and inject them into the main pipeline.In this post Im going to show 13 extensibility points that every ASP.NET MVC developer should know, starting from the beginning of the pipel

30、ine and going forward till the rendering of the view.1. RouteConstraintUsually you could put some constrains on url parameters using regular expressions, but if your constrains depend on something that is not only about the single parameter, you can implement the IRouteConstrainss method and put you

31、r validation logic in it.One example of this is the validation of a date: imagine an url that has year, month and date on different url tokens, and you want to be able to validate that the three parts make a valid date.·2. RouteHandlerNot really specific to ASP.NET MVC, the RouteHandler is the

32、component that decide what to do after the route has been selected. Obviously if you change the RouteHandler you end up handling the request without ASP.NET MVC, but this can be useful if you want to handle a route directly with some specific HttpHanlders or even with a classic WebForm.3. Controller

33、FactoryThe controller factory is the component that, based on the route, chooses which controller to instantiate and instantiate it. The default factory looks for anything that implements IController and whose name ends with Controller, and than create an instance of it through reflection, using the

34、 parameter-less constructor. But if you want to use Dependency Injection you cannot use it, and you have to use a IoC aware controller factory: there are already controller factory for most of the IoC containers. You can find them in MvcContrib or having a look at the Ninject Controller Factory.4. A

35、ctionInvokerActionInvoker is responsible for invoking the action based on its name. The default action invoker looks for the action based on the method name, the action name and possibly other selector attributes. Then it invokes the action method together with any filter defined  and finally i

36、t executes the action result.If you read carefully you probably understood that most of the execution pipeline is inside the logic of the default ControllerActionInvoker class. So if you want to change any of these conventions, from the action methods selection logic, to the way http parameters are

37、mapped to action parameters, to the way filters are chosen and executed, you have to extend that class and override the method you want to change.A good example of this, is the NinjectActionInvoker I developed to allow injection of dependencies inside filters.5. ActionMethodSelectorAttributeActions,

38、 with the default action invoker, are selected based on their name, but you can finer tune the selection of actions implementing your own Method Selector. The framework already comes with the AcceptVerbs attribute that allows you to specify to which HTTP Verb an action has to respond to.A possible s

39、cenario for a custom selector attribute is if you want to choose one action or another based on languages supported by the browser or based on the type of browser, for example whether it is a mobile browser or a desktop browser.6. AuthorizationFilterThese kind of filters are executed before the acti

40、on is executed, and their role is to make sure the request is “valid”.There are already a few Authorization filters inside the framework, the most “famous” of which is the Authorize attribute that checks whether the current user is allowed to execute the action. Another is the the ValidateAntiForger

41、yToken that prevents CSRF attacks. If you want to implement your own authorization schema, the interface you have to implement is IAuthorizationFilter. An example could be the hour of the day.7. ActionFilterAction Filters are executed before and after an action is executed. One of the core filters i

42、s the OutputCache filter, but you can find many other usages for this filter. This is the most likely extension point you are going to use, as, IMHO, its critical to a good componentization of views: the controller only has to do its main stuff, and all the other data needed by the view must be retr

43、ieved from inside action filters.8. ModelBinderThe default model binder maps HTTP parameters to action method parameters using their names: a http parameter named user.address.city will be mapped to the City property of the Address object that itself is a property of the method parameter named user.

44、 The DefaultModelBinder works also with arrays, and other list types.But it can be pushed even further: for example you might use it to convert the id of the person directly to the Person object looking up on the database. This approach is explained better in the following post Timothy Khouri (aka S

45、ingingEels): Model Binders in ASP.NET MVC. The code is based on the preview 5, but the concept remains the same.·9. ControllerBaseAll controllers inherit from the base class Controller. A good way to encapsulate logic or conventions inside your actions is to create you own layer supertype and h

46、ave all your controllers to inherit from it.·10. ResultFilterLike the ActionFiters, the ResultFilters are execute before and after the ActionResult is executed. Again, the OutputCache filter is an example of a ResultFilter. The usual example that is done to explain this filter is logging. If yo

47、u want to log that a page has been returned to the user, you can write a custom RenderFilter that logs after the ActionResult has been executed.·11. ActionResultASP.NET MVC comes with many different kind of results to render views, to render JSON, plain text, files and to redirect to other acti

48、ons. But if you need some different kind of result you can write your own ActionResult and implement the ExecuteResult method. For example, if you want to send a PDF file as result you could write your own ActionResult that use a PDF library to generate the PDF. Another possible scenario is the RSS

49、feed: read more about how to write a RssResult in this post.Look at implementing a custom action result when the only peculiarity is how the result is returned to the user.·12. ViewEngineProbably you are not going to write your own view engine, but there are a few that you might consider using

50、instead of the default WebForm view engine. The most interesting one, IMHO, is Spark.But if you really want to write your own view engine, have a look at this post by Brad Wilson: Partial Rendering & View Engines in ASP.NET MVC.·13. HtmlHelperViews must be very dumb and thin, and they shoul

51、d only have html markup and calls to HtmlHelpers. There should be no code inside the views, so helpers come very handy for extracting the code from the view and putting it into something that is testable. As Rob Conery says: “If there's an IF, make a Helper”.What is an HtmlHelper? Basically its

52、just an extension method of the HtmlHelper class, but thats the only requirement.本文摘自.nz/译文2ASP.NET MVC中你必须知道的13个扩展点 摘要ASP.NET MVC设计的主要原则之一是可扩展性。处理管线(processing pipeline)上的所有(或大多数)东西都是可替换的。因此,如果您不喜欢ASP.NET MVC所使用的约定(或缺乏某些约定),您可以创建自己的服务来支持您的约定,并将其注入到主管线中。在本文中,我们将从管线开始直到视图呈现,逐一向您展示每个ASP.NET MVC开发者都必须了

53、解13个扩展点。1.RouteConstraint通常情况下你可以使用正则表达式对url参数进行约束,但如果您的约束不仅仅取决于单一参数,您可以实现IRouteConstrains的方法,并在其中添加你的验证逻辑。比如对日期的验证,url中可能会包含年、月、日,而你需要验证这三者是否可以组合成一个有效的日期。2.RouteHandlerRouteHandler是在路由选择之后进行处理的组件,它并不仅仅针对ASP.NET MVC。显然,如果您改变了RouteHandler,那么对请求的处理将不再使用ASP.NET MVC,但这在您使用其他HttpHandler或经典的WebForm进行路由处理时

54、却是非常有用的。3.ControllerFactoryControllerFactory是基于路由的组件,它选择正确的controller并对其实例化。default factory会查找实现了IController并且以Controller结尾的类,然后通过反射使用无参构造函数进行实例化。但如果您希望使用依赖注入,就不能再使用default factory,而必须使用支持IoC的controller factory。MvcContrib和Ninject Controller Factory都包含支持IoC容器的controller factory。4.ActionInvokerActionI

55、nvoker顾名思义是负责调用(invoke)action的。默认的action invoker通过方法名、action名或其他可能的selector attribute来查找action,然后调用action方法以及定义的filter,最终执行得到action result。你会发现大部分执行管线存在于ControllerActionInvoker类的逻辑之中。因此,如果希望改变这些约定,如action方法的选择逻辑、http参数映射到action参数的方式、选择和执行filter的方式等,您需要扩展该类并重写需要修改的方法。可以参阅NinjectActionInvoker I developed to allow injection of dependencies inside filters。5.ActionMethodSelectorAttribute使用默认

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论