Icontrollerfactory and iactioninvoker of ASP. net mvc Extension

Source: Internet
Author: User

We all know that ASP. net mvc has been sought after by developers since its official launch.ArticleEven more, ASP. NET developers need to learn what ASP. net mvc says:

Webforms is a lie. It is an abstract mechanism surrounded by various lies and spoofing. Everything you do to webforms is irrelevant to the Web-it helps you do what you should have done by yourself.
Friends, this is a big deal (at least for me): you're working in a lie. The Web is in the "NONE" status, and it depends on something called HTML, and use another thing called HTTP to send HTML via a cable-you need to understand it, love it, and feel it in your bones.

 

ASP. net mvc is not a framework that binds your hands and feet, or an ASP. NET entry framework. you can completely control everything. In the web world, the UI has not been standardized to be controlled by the framework and is generated in a "standard" way.

 

All of these fully demonstrate the necessity of using the MVC Framework for web development. For more MVC and webforms features, you can refer to an article I accidentally found ASP. net MVC and ASP. net webforms features. For more information about the selectivity between the two, I would like to reserve some comments and only say that the right is the best. I would like to explain one thing in advance, you can select MVC for more control, but it also means you need to write moreCode(I Would Like ).

I recently used MVC development in a project, so I read the MVC source code. It is really great and highly scalable. It can be said that it can be expanded everywhere, simone chiaretta has an article translated: [translation] ASP.. Net MVC.

This article will introduce an application that extends icontrollerfactory and implement controller by extending defaultcontrollerfactory. the replacement of actioninvoker, and then extended iactioninvoker to return different viewresults. This article only implements some simple functions, and only serves as an example. Before reading this article, you need to familiarize yourself with the specific Asp.net MVC process. Although the preliminary study of the Asp.net MVC process is just a "preliminary study", it is sufficient to give you a rough understanding of the Asp.net MVC process.

First, we need to extend icontrollerfactory to replace actioninvoker with our own extended testcontrolleractioninvoker:

Public   Class Testcontrollerfactory: defaultcontrollerfactory
{

Protected   Override Icontroller getcontrollerinstance (type controllertype)
{
Icontroller =   Base . Getcontrollerinstance (controllertype ); // If dependency injection is used, you can obtain it from the injection container.

If ( Typeof (Controller). isassignablefrom (controllertype ))
{
Controller = Icontroller As Controller;

If (Controller ! =   Null )
Controller. actioninvoker =   New Testcontrolleractioninvoker (); // It can also be obtained from the injection container

Return Icontroller;
}
Return Icontroller;
}
}

 

Then expand iactioninvoker:

Public   Class Testcontrolleractioninvoker: controlleractioninvoker
{

Protected   Override Actionresult createactionresult (controllercontext, actiondescriptor, Object Actionreturnvalue)
{
If (Actionreturnvalue =   Null )
{
Return   New Xxxresult (); // You can process your own viewresult logic.
}

If(Typeof(Actionresult). isassignablefrom (actionreturnvalue. GetType ()))
ReturnActionreturnvalueAsActionresult;

Controllercontext. Controller. viewdata. Model=Actionreturnvalue;

//Viewresult can be returned Based on the returned type.
Return NewTttresult {viewdata=Controllercontext. Controller. viewdata, tempdata=Controllercontext. Controller. tempdata };
}
}

Here, we simply process the returned values after the action method is executed in the iactioninvoker pipeline. Of course, there are many iactioninvoker extensions, the advantage is that you do not need to focus too much on the view layer in the specific action, so the maintainability and scalability will become a level.

Finally, do not forget to set controllerfactory for controllerbuilder:

 

Controllerbuilder. Current. setcontrollerfactory ( New Testcontrollerfactory ());

Well, this article will briefly introduce this. I hope you can expand your project according to your own needs. If dependency injection is used in your project, to extend icontrollerfactory, you can refer to mvccontrib and ninject controller factory. for ASP. net MVC iactioninvoker-controlleractioninvoker the main task is to find action, call Action method and related filter, and execute to get actionresult. It can be said that there are a lot of Asp.net MVC execution logic, if you need to change these conventions, open the extension. Here is an example: ninjectactioninvoker I developed to allow injection of dependencies inside filters.

 

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.