【文章內(nèi)容簡介】
andler)的執(zhí)行。這個處理器將被安裝用于處理指定的擴展。在 IIS中, .aspx經(jīng)由“應用程序擴展”被映射到 ISAPI 的 dll文件: 。每一個觸發(fā) 的請求,都必須經(jīng)由一個已經(jīng)注冊的,并且指向 擴展名來標識。 注: ISAPI 是自定義 Web請求處理中第一個并且具有最高性能的 IIS入口點。 依靠擴展名, ,該處理器則負責處理這個請求。舉個例子, WebServices的擴展名 .asmx不會把一個請求路由到磁盤上的某一個頁面,而是會路由到在定義中附加了指定特性( WebMethodAttribute)的類,此特性會把它標識成一個 Web Services 的實現(xiàn)。許多其它的處理器將隨著 一起被安裝。當然也可以定義你自己的處理器。在 IIS里所有的 HttpHandler 被映射并指向 ISAPI 擴展,并且這些 HttpHandler也都在 ,用于把請求路由到指定的 HTTP處理器里執(zhí)行。每一個處理器都是一個 .NET類,用于處理指定的擴展。而這些處理器可以處理簡單到只有幾行代碼的 Hello World,也可以處理復雜到類似 的頁面以及執(zhí)行 WebService。就目前而言,僅僅需要理解擴展就是一種基本的映射機制, 用它可以從 ISAPI 里獲取一個請求,然后把請求路由到指定處理該請求的處理器中。 6 原文 2 Thirteen MVC extensibility points you have to know Abstract One of the main design principles MVC has been designed with is extensibility. Everything (or most of) in the processing pipeline is replaceable so, if you don’t like the conventions (or lack of them) that MVC uses, you can create your own services to support your conventions and inject them into the main pipeline. In this post I’m going to show 13 extensibility points that every 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, but if your constrains depend on something that is not only about the single parameter, you can implement the IRouteConstrains’s 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 want to be able to validate that the three parts make a valid date. 2. RouteHandler Not really specific to MVC, the RouteHandler is the ponent that decide what to do after the route has been selected. Obviously if you change the RouteHandler you end up handling the request without 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. ControllerFactory The controller factory is the ponent that, based on the route, chooses which controller to instantiate and instantiate it. The default factory looks for anything that implements IController 7 and whose name ends with Controller, and than create an instance of it through reflection, using the parameterless 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. ActionInvoker ActionInvoker is responsible for invoking the action based on it’s 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 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 ControllerActionInvoker class. So if you want to change any of these conventions, from the action method’s selection logic, to the way 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 example 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