关于控制器工厂的扩展,要么通过实现IControllerFactory接口,要么通过继承DefaultControllerFactory。本篇中,我想体验的是:

1、当请求经过路由,controller, action名称是以key/value键值对形式存放的,我们可以通过RequestContext.RouteData.Values["action"]和RequestContext.RouteData.Values["controller"]获取action或controller的名称。

2、通过实现IControllerFactory接口,根据请求中的controller名称,来返回不同类型的IController。

3、另外,当请求到了某个Controller, 通过实现IController接口,根据从路由中拿到的action名称,可以自定义响应。

□ HomeController:

        public ActionResult Index()
        {
            return Content("我来自NewProduct/Index");
        }

□ NewProductController:

using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
    public class NewProductController : Controller
    {
        public ActionResult Index()
        {
            return Content("我来自NewProduct/Index");
        }
 
    }
}

□ OldProductController:

通过实现IController接口,根据从路由中拿到的action名称,实现自定义响应。

using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
    public class OldProductController : IController
    {
        public void Execute(System.Web.Routing.RequestContext requestContext)
        {
            //action名称是以key/value保存的
            string actionName = requestContext.RouteData.Values["action"].ToString().ToLower();
            switch (actionName)
            {
                case "index":
                    requestContext.HttpContext.Response.Write("我来自OldProduct/Index");
                    break;
                default:
                    requestContext.HttpContext.Response.Write("我来自OleProduct/" + actionName);
                    break;
            }
        }
    }
}
 

□ MyControllerFactory

实现IControllerFactory接口,当controller名称是new的时候,返回NewProductController;当controller名称是old,返回OldProductController;默认情况下返回Home/Index。

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();
            }
        }
    }
}
 

全局注册自定义控制器工厂。

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

输入old/index:

输入old/any:

输入new/index:

输入new/any:

输入home/index:

输入any/any:

参考资料:

Controller Factory and Action Invoker Part 1

MVC扩展控制器工厂,通过实现IControllerFactory,根据action名称生成不同的Controller的更多相关文章

  1. MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject

    希望实现的效果是:对购物车中所有商品的总价,实现9折或8折: 当点击"9折": 当点击"8折": □ 思路 8折或9折是打折接口的不同实现,关键是:由什么条件决 ...

  2. MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串

    原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...

  3. MVC扩展控制器, 把部分视图转换成字符串(带验证信息), 并以json传递给前端视图

    当我们使用jQuery异步提交表单数据的时候,需要把部分视图转换成字符串(带验证信息),以json的形式传递给前端视图.   使用jQuery异步加载部分视图,返回内容追加到页面某个div:   jQ ...

  4. MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  5. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

  6. ASP.NET MVC 创建控制器类过程

    MvcHandler.ProcessRequestInit()方法: 1.1获取控制器的名称string requiredString = this.RequestContext.RouteData. ...

  7. Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”

    标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...

  8. MVC的控制器的激活过程,我们从MvcHandler开始讲,前面的事情以后再讲

    一.从MvcHandler开始(不要觉得是代码,让你看懂才是最重要的) using Microsoft.Web.Infrastructure.DynamicValidationHelper; usin ...

  9. 面向接口可扩展框架之“Mvc扩展框架及DI”

    面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...

随机推荐

  1. Codeforces 552C Vanya and Scales(进制转换+思维)

    题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...

  2. Vue-Socket.io

    github地址:https://github.com/MetinSeylan/Vue-Socket.io 安装: npm install vue-socket.io -S 注册: import Vu ...

  3. 好久没有写过SQL了,今天写了一句select in留存

    应同事要求,直接去接数据库的数据. 数据C里有一个name是查询的起始. 然后,B其实是一个多对多的中间表, 通过B查出id之后, 就可以在A里找到需要的数据了. select name from A ...

  4. 关于C语言的几个考试编程题目

    提交要求:1:邮件名称:学号后三位-题目编号-姓名-期中考试.例如:098-1-沈苗-期中考试2:不用附件提交,直接写邮件,内容包括编程思路(写一段自己对题目的认识.思路.技术细节等).源代码.运行结 ...

  5. 【AtCoder】AGC011 C - Squared Graph

    题解 大意是给出一张图,然后建一张新图,新图的点标号是(a,b) 如果a和c有一条边,b和d有一条边,那么(a,b)和(c,d)之间有一条边 我们把这道题当成这道题来做,给出两张图,如果第一张图有边( ...

  6. LoadRunner中log的使用总结

    LoadRunner中log的使用总结 1.log的设置方式. 在 runtime setting中可以设置log的生成方式: 默认的log方式: Enable logging选中,log optio ...

  7. 四、oracle 用户管理 二

    一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时,oracle会自动建立名称为default的profile.当建立用户没有指定profile选项时 ...

  8. 因为修改linux selinux修改错误产生的问题及解决办法

    会出现这个错误: not syncing attempted to kill init 解决办法是: 开机后一直按e 然后按这个修改: https://www.deep-silver.com/kern ...

  9. 解决mysql中limit和in不能同时使用的问题

    先给出数据表 CREATE TABLE `test_tb_grade` ( `ID` ) NOT NULL AUTO_INCREMENT, `) DEFAULT NULL, `COURSE` ) DE ...

  10. JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存

    1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...