13 extensions that you must know in ASP. net mvc, asp. netmvc

Source: Internet
Author: User

13 extensions that you must know in ASP. net mvc, asp. netmvc

ScottGu recommended the Simone Chiaretta Article 13 ASP in his latest blog. net mvc extensibility points you have to know. This article briefly introduces ASP.. net mvc. Keyvan Nayyeri (the book Beginning ASP. net mvc 1.0 co-authored with Simone) has 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 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

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

ActionFilter is executed before and after action execution. 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. His code is based on Preview 5, but his idea 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, ResultFilter 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.

 

Reference: http://www.cnblogs.com/kirinboy/archive/2009/06/01/13-asp-net-mvc-extensibility-points-you-have-to-know.html

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.