MVC extends the controller factory by implementing Icontrollerfactory

Source: Internet
Author: User

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

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.