13 Extensibility Points you need to know in MVC

Source: Internet
Author: User

The 13 extensibility points you must know in MVC pasting

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

ScottGu in its the latest blog post recommended in the Simone Chiaretta the article ASP. NET MVC Extensibility points you has to know , this article provides a brief introduction to the 13 extensibility points in ASP. Keyvan nayyeri(co-authored with Simone Beginning ASP 1.0 ) has published a number of articles, An in-depth discussion of these 13 extension points is carried out separately. I will translate these articles one by one in future essays, hoping to help you.

One of the main tenets of the ASP. NET MVC design is extensibility. All (or most) things on the processing pipeline (processing pipeline) are replaceable. Therefore, if you do not like the conventions used by ASP. NET MVC (or lack of some conventions), you can create your own services to support your contract and inject it into the lead line.

In this article, we'll start with the pipeline until the view renders, showing you each of the 13 extensibility points that each ASP developer must know.

1.RouteConstraint

Normally you can use regular expressions to constrain URL parameters, but if your constraints depend not only on a single parameter, you can implement the Irouteconstrains method and add your validation logic to it.

For example, the validation of the date, the URL may contain the year, month, day, and you need to verify whether the three can be combined into a valid date.

2.RouteHandler

Routehandler is a component that is processed after routing is selected, and it is not just for ASP. Obviously, if you change the Routehandler, the processing of the request will no longer use ASP. NET MVC, but this is useful when you are routing with other HttpHandler or classic WebForm.

3.ControllerFactory

Controllerfactory is a routing-based component that chooses the correct controller and instantiates it. The default factory looks for classes that implement IController and ends with a controller, and then instantiates using the parameterless constructor through reflection.

However, if you want to use dependency injection, you can no longer use the default factory, and you must use a controller factory that supports IOC. Both the Mvccontrib and Ninject controller factory contain controller factory that support the IOC container.

4.ActionInvoker

Actioninvoker, as the name implies, is responsible for invoking (invoke) action. The default action Invoker finds the action by using the method name, action name, or other possible selector attribute, and then invokes the action method and the defined filter, resulting in 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, how the HTTP parameters map to the action parameter, how to select and execute the filter, and so on, you need to extend the class and override the methods that need to be modified.

See Ninjectactioninvoker I developed to allow injection of dependencies inside filters.

5.ActionMethodSelectorAttribute

When you use the default action Invoker, the selection of the action is name-based. You can also implement your own method selector to improve the choice of action. The Acceptverbs feature is already included in the framework, which allows you to specify which HTTP verb to use to handle the response of the action.

For example, you might want to select an action based on the language or browser type supported by the browser, such as a mobile device's browser or desktop browser.

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, most notably the authorize feature, which is used to check whether the current user is allowed to execute the action. The other is the Validateantiforgerytoken used to stop CSRF attacks. If you want to implement your own authorization, you must implement an interface. For example, the hour in a date.

7.ActionFilter

Action filters is executed before and after action execution. The OutputCache filter is one of several core filters. This may be the extension point you are most likely to use, and in my opinion, the controller is only concerned with its primary work, and all other data required by view must be obtained from within the action filter, which is critical for an organization-good view.

8.ModelBinder

The default model binder uses the parameter name to map the HTTP parameter to the action method parameter. For example, the HTTP parameter user.address.city maps to the city property of the Address property of the method parameter user. Defaultmodelbinder also applies to arrays and other list types.

Further, for example, you might want to retrieve from a database and convert it directly to the person object based on the ID of the person. Timothy Khouri (Network name Singingeels) illustrates this approach better in his article model binders in ASP. His code is based on preview 5, but the idea is the same.

9.ControllerBase

All controllers inherit from the base class controller. To encapsulate your own logic and conventions in action, it's a good way to create your own parent class so that all controllers inherit from that 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, a more common example of this type of filter is log logging. If you want the log to be logged when the page is returned to the user, you can write a custom renderfilter to log after ActionResult executes.

11.ActionResult

ASP. NET MVC provides a lot of result to render views, JSON, plain text, files, and redirects to other actions. If you need other types of result, 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 use the PDF library to write a actionresult that generates a PDF. As an RSS feed, see How to write a rssresult in this post.

12.ViewEngine

You may not need to write your own view engine, but you may want to consider using a different engine instead of the default WebForm view engine. In my opinion, the most interesting engine is spark.

If you really want to write your own view engine, take a look at Brad Wilson's article: Partial Rendering & View Engines in ASP.

13.HtmlHelper

Views must be very simple and neat, they can only contain HTML tags and call HtmlHelper's helper methods. The view cannot contain any code, so the helper method must be very handy, allowing you to extract the code from the view and put it in a testable environment. As Rob Conery said, if there is an if, construct an auxiliary method (if there ' s an if, make a Helper).

What is the HtmlHelper helper method? 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 a view.

Which of these should be used in your application?

As you might guess, not all applications need to extend the 13 extension points above. The most likely extensions in all applications are Actionfilter and HtmlHelper. In addition, you will most likely use extensions written by others, such as controllerfactory using the IOC container or viewengine to get rid of WebForm.

However, it is important to learn these extensibility points and try them out so that you can make a choice and be ready to use these powerful extensibility points whenever necessary. Next week I will publish some articles explaining how to use these extensibility points.

If you would like to learn more about this topic, consider buying the upcoming beginning ASP. NET MVC (I am one of the authors) or Professional ASP (ASP. NET MVC Development Team) or ASP. In Action (Jeffrey Palermo and Ben Scheirman).

Did I miss out on some extension points that you think are important? Have you used the extension points I mentioned above? I'd love to hear the scene you're in.

13 Extensibility Points you need to know in MVC

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.