One of the main principles of ASP. net mvc design is scalability. All (or most) things on the processing pipeline (processing pipeline) are replaceable. Therefore, if you do not like ASP. net MVC conventions (or lack of some conventions), you can create your own services to support your conventions and inject them into the supervisor line.
In this article, we will show you the 13 extensions that every ASP. net mvc developer must understand from the pipeline to the view.
1. routeconstraint of ASP. net mvc Extension
Generally, you can use a regular expression to restrict URL parameters. However, if your constraints depend not only on a single parameter, You can implement the irouteconstrains method and add your verification logic to it. For example, for date verification, the URL may contain year, month, and day, and you need to verify whether the three can be combined into a valid date.
2. routehandler of ASP. net mvc Extension
Routehandler is a component processed after a route is selected. It is not only for ASP. net mvc. Obviously, if you change routehandler, ASP will not be used for request processing. net MVC, but this is very useful when you use other httphandler or classic webform for routing.
3. controllerfactory of ASP. net mvc Extension
Controllerfactory is a routing-based component that selects the correct controller and instantiates it. The default factory searches for classes that implement the icontroller and end with the Controller, and then instantiates the class using a non-argument constructor through reflection. However, if you want to use dependency injection, you cannot use the default factory. Instead, you must use the Controller factory that supports IOC. Mvccontrib and ninject controller factory both contain controller factory that supports IOC containers.
4. ASP. net mvc extension-actioninvoker
As the name suggests, actioninvoker is responsible for calling (invoke) action. The default action invoker searches for the action by the method name, action name, or other possible selector attribute, then calls the action method and the defined filter, and finally executes the action result.
You will find that most of the execution pipelines exist in the logic of the controlleractioninvoker class. Therefore, if you want to change these conventions, such as the selection logic of the Action method, the method for ing HTTP parameters to the action parameter, and the method for selecting and executing the filter, you need to extend the class and override the method to be modified. See ninjectactioninvoker I developed to allow injection of dependencies inside filters.
5. ASP. net mvc extension-actionmethodselectorattribute
When the default action invoker is used, the action is selected based on the name. You can also implement your method selector to improve the choice of action. The framework already contains the acceptverbs feature, which allows you to specify which HTTP verb is used to process the action response.
For example, you may want to select actions based on the language or browser type supported by the browser (such as the browser or desktop browser of a mobile device.
6. ASP. net mvc extension authorizationfilter
This filter is executed before the action is executed to ensure that the request is valid. The framework already contains some autorization filters. The most famous one is the authorize feature, which is used to check whether the current user can execute this action. The other is the validateantiforgerytoken used to prevent csrf attacks. If you want to implement your own authorization, you must implement the interface. For example, the hour in the date.
7. ASP. net mvc extension-actionfilter
Action filters is executed before and after the action is executed. The outputcache filter is one of several core filters. This may be the most likely extension point you are using, and in my opinion, the Controller only cares about its main work, all other data required by the view must be obtained from the action filter. This implementation is critical for a well-organized view.
8. modelbinder of ASP. net mvc Extension
The default model binder uses the parameter name to map HTTP parameters to action parameters.
For example, the HTTP parameter user. Address. city maps to the city attribute of the address attribute of the method parameter user.
Defaultmodelbinder also applies to arrays and other list types. Further, for example, you may want to retrieve from the database and convert it to the person object based on the person ID. Timothy KHOURI (Network Name singingeels) elaborated on this method in his article model binders in ASP. net mvc. His code is based on preview 5, but his idea is the same.
9. controllerbase of ASP. net mvc Extension
All controllers are inherited from the base class controller. To encapsulate your own logic and conventions in action, it is a good way to create your own parent class so that all controllers inherit from the class.
10. ASP. net mvc extension-resultfilter
Similar to actionfilter, resultfilters is executed before and after actionresult. The outputcache filter can also be used as an example of resultfilter. In addition, log records are commonly used to interpret such filters. If you want to record the log when the page is returned to the user, you can write a custom renderfilter to record the log after the actionresult is executed.
11. actionresult of ASP. net mvc Extension
ASP. net mvc provides many results for rendering views, JSON, plain text, files, and redirecting to other actions. If you need other types of results, you can customize the actionresult and implement the executeresult method. For example, if you want to send a PDF file as a result, you need to write an actionresult that can generate a PDF file in the PDF library.
12. ASP. net mvc extension viewengine
You may not need to write your own view engine, but you may consider using other engines to replace the default webform view engine. In my opinion, the most interesting engine is spark. If you really want to write your own view engine, read Brad Wilson's article: Partial rendering & view engines in ASP. net mvc.
13. htmlhelper of ASP. net mvc Extension
Views must be very simple and tidy. They can only contain HTML tags and call the auxiliary methods of htmlhelper. The view cannot contain any code, so the auxiliary method must be very convenient, so that you can extract the code from the view and put it in a testable environment. As Rob conery said: if there is if, construct the Helper method (if there's an IF, make a helper ). What is the auxiliary htmlhelper method? It is actually the extension method of the htmlhelper class, which is the only requirement. You can learn from Rob's article avoiding tag soup why htmlhelper is a good way to encapsulate code in the view.
Which one should you use in your application? As you have guessed, not all applications need to expand the above 13 extension points. Actionfilter and htmlhelper are most likely to be extended in all applications. In addition, you are likely to use extensions compiled by others, such as using the controllerfactory of the IOC container or viewengine used to get rid of webform. However, it is very important to learn and try these extensions so that you can make choices and be ready to use them whenever necessary.