ASP论文外文翻译---从底层了解ASP.NET的结构.doc

上传人:小小飞 文档编号:26419 上传时间:2018-10-29 格式:DOC 页数:13 大小:69.01KB
返回 下载 相关 举报
ASP论文外文翻译---从底层了解ASP.NET的结构.doc_第1页
第1页 / 共13页
ASP论文外文翻译---从底层了解ASP.NET的结构.doc_第2页
第2页 / 共13页
ASP论文外文翻译---从底层了解ASP.NET的结构.doc_第3页
第3页 / 共13页
ASP论文外文翻译---从底层了解ASP.NET的结构.doc_第4页
第4页 / 共13页
ASP论文外文翻译---从底层了解ASP.NET的结构.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《ASP论文外文翻译---从底层了解ASP.NET的结构.doc》由会员分享,可在线阅读,更多相关《ASP论文外文翻译---从底层了解ASP.NET的结构.doc(13页珍藏版)》请在三一文库上搜索。

1、1 原文 1 A low-level Look at the ASP.NET Architecture Abstract ASP.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

2、WebForms 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.NET Lets

3、 start 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.Its much more than just WebForms and Web Services ASP.NET is a request processing engine. It takes an incoming requ

4、est 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 I

5、IS or 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

6、 process, 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 si

7、gnature 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 completel

8、y built 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

9、is the thoughtful design that makes the architecture easy to work with, yet provides hooks into just about any part of the request processing. 2 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

10、say ASP 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 som

11、e time to 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

12、and ASP.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 Connecti

13、on ISAPI is 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 tha

14、t developers 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

15、.NET both 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 leve

16、l applications, 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 p

17、lumbing. For 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 b

18、oth ISAPI extensions 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 abil

19、ity to look 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 detai

20、l. 3 ISAPI is 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- 4 译文 1 从底层了解 ASP.NET的结构 摘要 ASP.NET是一个用于构建 Web程序的强大平台,提供了强大的柔性和能力以至于它可 以构建任意的 Web程序。许多人仅仅对处于 AS

21、P.NET高层次的框架如:WebForms 和 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 运行时提供了一套复杂的,但却很优雅的机制。在处理请求的每一个层 面都牵涉到许多对象,但大多数对象都可以通过派生或者事件接口来扩展。所以,此框架 具有非常高的可扩展性。通过这一套机制,可以进入较低层次的接口如:缓存,身份验证, 授权等是有可能的。你可以在处理请求之前或之后过滤内容,或者仅仅把匹配指定签

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

24、之前这些任务是使用 IIS上的 ISAPI扩展和过滤来 完成的。尽管还有一些限制,但与 ASP相比,已经有了很大的进步。ISAPI 是底层 Win32 样式的 API,仅它的接口就有 1兆,这对于大型的程序开发是非常困难的。由于 ISAPI是 底层的接口,因此它的速度也是非常的快。但对于企业级的程序开发是相当的难于管理的。 5 所以,在一定的时间内,ISAPI 主要充当其它应用程序或平台的桥接口。但是无论如何, ISAPI没有被废弃。事实上,微软平台上的 ASP.NET和 IIS的接口是通过宿主在.NET 里的 ISAPI扩展来通信的,然后直达 ASP.NET运行时。ISAPI 提供了与 We

25、b Server通信的核心 接口,然后 ASP.NET使用非托管代码获取请求以及对客户端请求发出响应。ISAPI 提供的 内容经由公共对象类似于 HttpRequest和 HttpResponse,通过一个设计优良的、可访问的 接口,以托管对象的方式暴露非托管数据。 2 从浏览器到 ASP.NET 让我们从一个典型的 ASP.NET Web请求的生命周期的起点开始。用户通过在浏览器中 键入一个 URL,点击一个超链接,提交一个 HTML表单(一个 post请求),或者一个客户 端程序调用基于 ASP.NET的 WebService(通过 ASP.NET提供服务)。在服务器端,IIS5 或 者

26、IIS6将会收到这个请求。ASP.NET 的底层通过 ISAPI扩展与 IIS通信,然后,通过 ASP.NET,这个请求通常被路由到一个带有.aspx 扩展名的页面。但是,这个处理过程如何 工作,则完全依赖于 HTTP处理器(handler)的执行。这个处理器将被安装用于处理指定 的扩展。在 IIS中,.aspx 经由“应用程序扩展”被映射到 ASP.NET ISAPI的 dll文件: aspnet_isapi.dll。每一个触发 ASP.NET的请求,都必须经由一个已经注册的,并且指向 aspnet_isapi.dll的扩展名来标识。 注: ISAPI是自定义 Web请求处理中第一个并且具有

27、最高性能的 IIS入口点。 依靠扩展名,ASP.NET 把一个请求路由到一个恰当的处理器,该处理器则负责处理这 个请求。举个例子,WebServices 的扩展名.asmx 不会把一个请求路由到磁盘上的某一个页 面,而是会路由到在定义中附加了指定特性(WebMethodAttribute)的类,此特性会把它 标识成一个 Web Services的实现。许多其它的处理器将随着 ASP.NET一起被安装。当然也 可以定义你自己的处理器。在 IIS里所有的 HttpHandler被映射并指向 ASP.NET ISAPI扩 展,并且这些 HttpHandler也都在 web.config里配置,用于把

28、请求路由到指定的 HTTP处 理器里执行。每一个处理器都是一个.NET 类,用于处理指定的扩展。而这些处理器可以处 理简单到只有几行代码的 Hello World,也可以处理复杂到类似 ASP.NET的页面以及执行 WebService。就目前而言,仅仅需要理解扩展就是一种基本的映射机制,ASP.NET 用它可 以从 ISAPI里获取一个请求,然后把请求路由到指定处理该请求的处理器中。 6 原文 2 Thirteen ASP.NET MVC extensibility points you have to know Abstract One of the main design princip

29、les ASP.NET MVC has been designed with is extensibility. Everything (or most of) in the processing pipeline is replaceable so, if you dont like 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

30、. In this post Im going to show 13 extensibility points that every ASP.NET MVC developer should know, starting from the beginning of the pipeline and going forward till the rendering of the view. 1. RouteConstraint Usually you could put some constrains on url parameters using regular expressions, bu

31、t if your constrains depend on something that is not only about the single parameter, you can implement the IRouteConstrainss method and put your 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 wan

32、t to be able to validate that the three parts make a valid date. 2. RouteHandler Not really specific to ASP.NET MVC, the RouteHandler is the 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 MV

33、C, but this can be useful if you want to handle a route directly with some specific HttpHanlders or even with a classic WebForm. 3. ControllerFactory The controller factory is the component that, based on the route, chooses which controller to instantiate and instantiate it. The default factory look

34、s for anything that implements IController 7 and whose name ends with Controller, and than create an instance of it through reflection, using the 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 a

35、lready controller factory for most of the IoC containers. You can find them in MvcContrib or having a look at the Ninject Controller Factory. 4. ActionInvoker ActionInvoker is responsible for invoking the action based on its name. The default action invoker looks for the action based on the method n

36、ame, the action name and possibly other selector attributes. Then it invokes the action method together with any filter defined and finally it executes the action result. If you read carefully you probably understood that most of the execution pipeline is inside the logic of the default ControllerAc

37、tionInvoker class. So if you want to change any of these conventions, from the action methods selection logic, to the way http parameters are 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 exam

38、ple of this, is the NinjectActionInvoker I developed to allow injection of dependencies inside filters. 5. ActionMethodSelectorAttribute Actions, 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.

39、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 scenario 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

40、 browser, for example whether it is a mobile browser or a desktop browser. 6. AuthorizationFilter These kind of filters are executed before the action is executed, and their role is to make sure the request is “valid”. 8 There are already a few Authorization filters inside the framework, the most “f

41、amous” of which is the Authorize attribute that checks whether the current user is allowed to execute the action. Another is the the ValidateAntiForgeryToken that prevents CSRF attacks. If you want to implement your own authorization schema, the interface you have to implement is IAuthorizationFilte

42、r. An example could be the hour of the day. 7. ActionFilter Action Filters are executed before and after an action is executed. One of the core filters is 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, IMH

43、O, 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 retrieved from inside action filters. 8. ModelBinder The default model binder maps HTTP parameters to action method parameters using their names: a htt

44、p 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. 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

45、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 SingingEels): Model Binders in ASP.NET MVC. The code is based on the preview 5, but the concept remains the same. 9. ControllerBase All controlle

46、rs 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 have all your controllers to inherit from it. 10. ResultFilter Like the ActionFiters, the ResultFilters are execute before and after the ActionResult

47、 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 you 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. 9 11. A

48、ctionResult ASP.NET MVC comes with many different kind of results to render views, to render JSON, plain text, files and to redirect to other actions. 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

49、 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 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. ViewEngine Probably you are not going to write your own view engine, but there are a few that you might consider using instead of the default WebForm view engine. The most intere

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1