13 extensions you must know in ASP. NET MVC

Source: Internet
Author: User
ArticleDirectory
    • 1. routeconstraint
    • 2. routehandler
    • 3. controllerfactory
    • 4. actioninvoker
    • 5. actionmethodselectorattribute
    • 6. authorizationfilter
    • 7. actionfilter
    • 8. modelbinder
    • 9. controllerbase
    • 10. resultfilter
    • 11. actionresult
    • 12. viewengine
    • 13. htmlhelper
    • Which one should you use in your application?

ScottguIn itsLatest blogIs recommendedSimone chiarettaArticle13 ASP. net mvc extensibility points you have to knowThis article briefly introduces 13 extension points in ASP. net mvc.Keyvan nayyeri(Co-authored with SimoneBeginning ASP. net mvc 1.0I have published several articles to discuss these 13 extension points in depth. I will translate these articles one by one in the future, hoping to help you.

 

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

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

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 no longer be used for request processing. net MVC, but this is very useful when you use other httphandler or classic webform for routing.

3. controllerfactory

Controllerfactory is a routing-based component that selects the correct controller and instantiates it. Default factory will find and implementIcontrollerClasses ending with controller are then instantiated 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. 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. 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. 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. 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

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. HisCodeIt is based on preview 5, but its philosophy is the same.

9. controllerbase

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. 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

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. For another example, see how to write a rssresult in this post.

12. 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

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. I will post some articles next week to illustrate how to use these extensions.

If you want to learn more about this topic, you can purchase the upcoming beginning ASP. net MVC (I am one of the authors) or professional ASP. net MVC (Asp. net MVC development team) or ASP. net MVC in Action (by Jeffrey Palermo and Ben scheirman ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.