MVC extends the Controller factory. By implementing IControllerFactory, different controllers are generated based on the action name.

Source: Internet
Author: User

The extension of the controller factory either implements the IControllerFactory interface or inherits the DefaultControllerFactory. In this article, I want to experience:

 

1. When the request is routed, the controller and action names are stored in key/value pairs. We can use RequestContext. routeData. values ["action"] And RequestContext. routeData. values ["controller"] gets the name of the action or controller.

 

2. Implement the IControllerFactory interface to return different types of icontrollers Based on the controller name in the request.

 

3. When a request arrives at a Controller, you can customize the response based on the action name obtained from the route by implementing the IController interface.

 

□Homecontroller:

        public ActionResult Index()
        {
Return Content ("I'm from NewProduct/Index ");
        }

 

□Newproductcontroller:

using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
    public class NewProductController : Controller
    {
        public ActionResult Index()
        {
Return Content ("I'm from NewProduct/Index ");
        }
 
    }
}

 

□Oldproductcontroller:

Implement the IController interface to implement custom response based on the action name obtained from the route.

using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
    public class OldProductController : IController
    {
        public void Execute(System.Web.Routing.RequestContext requestContext)
        {
// The action name is saved as key/value
            string actionName = requestContext.RouteData.Values["action"].ToString().ToLower();
            switch (actionName)
            {
                case "index":
RequestContext. HttpContext. Response. Write ("I am from OldProduct/Index ");
                    break;
                default:
RequestContext. HttpContext. Response. Write ("I am from OleProduct/" + actionName );
                    break;
            }
        }
    }
}
 

 

□Mycontrollerfactory

Implement the IControllerFactory interface. When the controller name is new, NewProductController is returned. When the controller name is old, OldProductController is returned. By default, Home/Index is returned.

using System;
using System.Web.Mvc;
using System.Web.SessionState;
using MvcApplication1.Controllers;
 
namespace MvcApplication1.Extension
{
    public class MyControllerFactory : IControllerFactory
    {
        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            Type controllerType = null;
            switch (controllerName)
            {
                case "new":
                    controllerType = typeof (NewProductController);
                    break;
                case "old":
                    controllerType = typeof (OldProductController);
                    break;
                default:
                    controllerType = typeof (HomeController);
                    requestContext.RouteData.Values["controller"] = "Home";
                    requestContext.RouteData.Values["action"] = "index";
                    break;
            }
            return controllerType == null ? null : (IController) DependencyResolver.Current.GetService(controllerType);
        }
 
        public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }
 
        public void ReleaseController(IController controller)
        {
            IDisposable disposable = controller as IDisposable;
            if (disposable != null)
            {
                disposable.Dispose();
            }
        }
    }
}
 

 

Register the custom controller factory globally.

        protected void Application_Start()
        {
            ......
 
            ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
        }
 

 

Enter old/index:
Enter old/any:
Enter new/index:
Enter new/any:
Enter home/index:
Input any/any:
References:

Controller Factory and Action Invoker Part 1

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.