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