Extensions to the Controller factory either by implementing the Icontrollerfactory interface or by inheriting the defaultcontrollerfactory. In this article, I want to experience:
1, when the request is routed, controller, the action name is stored in the form of Key/value key value, we can requestcontext.routedata.values["action" and requestcontext.routedata.values["Controller"] gets the name of the action or controller.
2, by implementing the Icontrollerfactory interface, according to the controller name in the request, to return different types of IController.
3, in addition, when the request to a controller, by implementing the IController interface, according to the action name obtained from the route, you can customize the response.
-homecontroller:
Public ActionResult Index ()
{
return Content ("I come from Newproduct/index");
}
-newproductcontroller:
using SYSTEM.WEB.MVC;
namespace Mvcapplication1.controllers
{
Public class Newproductcontroller:controller
{
Public ActionResult Index ()
{
return Content ("I come from Newproduct/index");
}
}
}
-oldproductcontroller:
By implementing the IController interface, a custom response is implemented 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 in Key/value
string actionname = requestcontext.routedata.values["Action"]. ToString (). ToLower ();
Switch (ActionName)
{
Case "Index":
RequestContext.HttpContext.Response.Write ("I come from Oldproduct/index");
break;
default:
RequestContext.HttpContext.Response.Write ("I come from oleproduct/" + actionname);
break;
}
}
}
}
-mycontrollerfactory
Implements the Icontrollerfactory interface, returns Newproductcontroller when the controller name is new, and returns Oldproductcontroller when the controller name is old ; Home/index is returned by default.
using System;
using SYSTEM.WEB.MVC;
using System.Web.SessionState;
using Mvcapplication1.controllers;
namespace Mvcapplication1.extension
{
Public class Mycontrollerfactory:icontrollerfactory
{
Public string controllername)
{
null;
Switch (controllername)
{
Case "New":
typeof (Newproductcontroller);
break;
Case "Old":
typeof (Oldproductcontroller);
break;
default:
typeof (HomeController);
requestcontext.routedata.values["Controller""Home";
requestcontext.routedata.values["Action""index";
break;
}
return NULL null : (IController) DependencyResolver.Current.GetService (Controllertype);
}
Public string controllername)
{
return Sessionstatebehavior.default;
}
Public void Releasecontroller (IController Controller)
{
as IDisposable;
if null)
{
Disposable. Dispose ();
}
}
}
}
Globally registers the custom controller factory.
protected void Application_Start ()
{
......
ControllerBuilder.Current.SetControllerFactory (new mycontrollerfactory ());
}
Input Old/index:
Input Old/any:
Input New/index:
Input New/any:
Input Home/index:
Input Any/any:
Resources:
Controller Factory and Action Invoker Part 1