【ASP.NET MVC 学习笔记】- 11 Controller和Action(2)

本文参考:http://www.cnblogs.com/willick/p/3331513.html

1、MVC一个请求的发出至action返回结果的流程图如下:

   

 

    重点是Controller FactoryAction Invoker。Controller Factory的作用是创建为请求提供服务的Controller实例;Action Invoker的作用是寻找并调用Action方法。

2、自定义Controller Factory需要实现IControllerFactory接口(实际开发中不建议这样做)。示例如下:

public class CustomControllerFactory : IControllerFactory 
{
//当MVC框架需要一个 Controller 来处理请求时调用
public IController CreateController(RequestContext requestContext, string controllerName) { Type targetType = null; switch (controllerName) { case "Product": targetType = typeof(ProductController); break; case "Customer": targetType = typeof(CustomerController); break; default:
//在自定义的Cotroller Factory中,我们可以任意改变系统默认的行为。下列语句将路由的controller值改为Product,使得执行的cotroller并不是用户所请求的controller requestContext.RouteData.Values[
"controller"] = "Product"; targetType = typeof(ProductController); break; } return targetType == null ? null : (IController)DependencyResolver.Current.GetService(targetType); } public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) { return SessionStateBehavior.Default; } public void ReleaseController(IController controller) { IDisposable disposable = controller as IDisposable; if (disposable != null) { disposable.Dispose(); } } }

    获取Controller实例:

//静态的 DependencyResolver.Current 属性返回一个 IDependencyResolver 接口的实现,这个实现中定义了 GetService 方法,它根据 System.Type 对象(targetType)参数自动为我们创建 targetType 实例
return targetType == null ? null : (IController)DependencyResolver.Current.GetService(targetType);

//要使用自定义的Controller Factory还需要在 Global.asax.cs 文件的 Application_Start 方法中对自定义的 CustomControllerFactory 类进注册
protected void Application_Start() 
{
    ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
}

3、使用内置的DefaultControllerFactory,当它从路由系统接收到一个请求,从路由实例中解析出controller名称,然后根据名称寻找controller类,这个controller类必须满足一下条件:

  • 必须是public。
  • 必须是具体的类(非抽象类)。
  • 没有泛型参数。
  • 类的名称必须以Controller结尾。
  • 类必须(间接或直接)实现IController接口。

   DefaultControllerFactory类维护了一个满足以上标准的类的列表,这样当每次接收到一个请求时不需要再去搜索一遍。当它找到了合适的 controller 类,则使用Controller Activator(一会介绍)来创建Controller 类的实例。其内部是通过 DependencyResolver 类进行依赖解析创建 controller 实例的。

   DefaultControllerFactory可被重写方法有:

  • GetControllerType,返回Type类型,为请求匹配对应的 controller 类,用上面定义的标准来筛选 controller 类也是在这里执行的。
  • GetControllerInstance,返回是IController类型,作用是根据指定 controller 的类型创建类型的实例。
  • CreateController 方法,返回是 IController 类型,它是 IControllerFactory 接口的 CreateController 方法的实现。默认情况下,它调用 GetControllerType 方法来决定哪个类需要被实例化,然后把controller 类型传递给GetControllerInstance。

   重写DefaultControllerFactory的 GetControllerInstance 方法,可以实现对创建 controller 实例的过程进行控制,最常见的是进行依赖注入。

4、当 DefaultControllerFactory 类接收到一个 controller 实例的请求时,在 DefaultControllerFactory 类内部通过 GetControllerType 方法来获得 controller 的类型,然后把这个类型传递给 GetControllerInstance 方法以获得 controller 的实例。

   所以在 GetControllerInstance 方法中就需要有某个东西来创建 controller 实例,这个创建的过程就是 controller 被激活的过程。

   默认情况下 MVC 使用 DefaultControllerActivator 类来做 controller 的激活工作,它实现了 IControllerActivator 接口。

   自定义ControllerActivator示例:

//如果请求的是 ProductController 则我们给它创建 CustomerController 的实例
public class CustomControllerActivator : IControllerActivator 
{
    public IController Create(RequestContext requestContext, Type controllerType) 
     {
        if (controllerType == typeof(ProductController)) {
            controllerType = typeof(CustomerController);
        }
        return (IController)DependencyResolver.Current.GetService(controllerType);
    }
}

//注册
protected void Application_Start() 
{ 
    ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new CustomControllerActivator())); 
} 

5、当 Controller Factory 创建好了一个类的实例后,MVC框架则需要一种方式来调用这个实例的 action 方法。如果创建的 controller 是继承 Controller 抽象类的,那么则是由 Action Invoker 来完成调用 action 方法的任务,MVC 默认使用的是 ControllerActionInvoker 类。如果是直接继承 IController 接口的 controller,那么就需要手动来调用 action 方法。自定义Action Invoker(一般不建议这么做)示例:

    public class CustomActionInvoker : IActionInvoker 
    {
//如果请求的是Index Action,这个 Invoker 通过 Response 直接输出一个消息,如果不是请Index Action,则会引发一个404-未找到错误
public bool InvokeAction(ControllerContext controllerContext, string actionName) { if (actionName == "Index") { controllerContext.HttpContext.Response.Write("This is output from the Index action"); return true; } else { return false; } } }

    创建一个ActionInvokerController,并在它的构造函数中指定了 Action Invoker 为我们自定义的 Action Invoker:

public class ActionInvokerController : Controller 
{
        public ActionInvokerController() 
        {
            this.ActionInvoker = new CustomActionInvoker();
        }
    }

6、通过自定义 Action Invoker,我们知道了MVC调用 Action 方法的机制。我们创建一个继承自 Controller 抽象类的 controller,如果不指定Controller.ActionInvoker,那么MVC会使用内置默认的Action Invoker,它是 ControllerActionInvoker 类。它的工作是把请求匹配到对应的 Action 方法并调用之,简单说就是寻找和调用 Action 方法。Action方法必须满足:

  • 必须是公共的(public)。
  • 不能是静态的(static)。
  • 不能是System.Web.Mvc.Controller中存在的方法,或其他基类中的方法。如方法不能是 ToString 和 GetHashCode 等。
  • 不能是一个特殊的名称。所谓特殊的名称是方法名称不能和构造函数、属性或者事件等的名称相同。
  • 不能带有泛型。

7、默认情况下,内置的Action Invoker (ControllerActionInvoker)寻找的是和请求的 action 名称相同的 action 方法。比如路由系统提供的 action 值是 Index,那么 ControllerActionInvoker 将寻找一个名为 Index 的方法,如果找到了,它就用这个方法来处理请求。ControllerActionInvoker 允许我们对此行为进行调整,即可以通过使用 ActionName 特性对 action 使用别名,如下对 CustomerController 的 List action 方法使用 ActionName 特性:

public class CustomerController : Controller 
{
    //当请求 Enumerate Action 时,将会使用 List 方法来处理请求。请求/Customer/List 会报错
    [ActionName("Enumerate")]
    public ViewResult List() 
   {
        return View("Result", new Result {
            ControllerName = "Customer",
            ActionName = "List"
        });
    }
}

    使用 Action 方法别名有两个好处:一是可以使用非法的C#方法名来作为请求的 action 名,如 [ActionName("User-Registration")]。二是,如果你有两个功能不同的方法,有相同的参数相同的名称,但针对不同的HTTP请求(一个使用 [HttpGet],另一个使用 [HttpPost]),你可以给这两个方法不同的方法名,然后使用 [ActionName] 来指定相同的 action 请求名称。

8、我们经常会在 controller 中对多个 action 方法使用同一个方法名。在这种情况下,我们就需要告诉 MVC 怎样在相同的方法名中选择正确的 action 方法来处理请求。这个机制称为 Action 方法选择,它在基于识别方法名称的基础上,允许通过请求的类型来选择 action 方法。MVC 框架可使用C#特性来做到这一点,所以这种作用的特性可以称为 Action 方法选择器

   MVC提供了几种内置的特性来支持 Action 方法选择,它包括HttpGetHttpPostHttpPutNonAction 等。

9、除了使用内置的Action方法选择器外,我们也可以自定义。所有的 action 选择器都继承自 ActionMethodSelectorAttribute 类,这个类的定义如下:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
    public abstract class ActionMethodSelectorAttribute : Attribute 
    { 
        public abstract bool IsValidForRequest(ControllerContext controllerContext,  MethodInfo methodInfo); 
    } 

    以下示例演示自定义Action选择器,实现同一个URL,本地和远程请求不同的Action方法。

//定义Attribute
public class LocalAttribute : ActionMethodSelectorAttribute 
{
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
       {
            return controllerContext.HttpContext.Request.IsLocal;
        }
 }

//使用
public class CustomerController : Controller 
{
        
    public ViewResult Index() 
    {
        return View("Result", new Result 
        {
            ControllerName = "Customer",
            ActionName = "Index"
        });
    }
    
//不加Local,如果请求 /Customer/Index,这两个 action 方法都会被匹配到而引发歧义问题,程序将会报错。
[Local] [ActionName(
"Index")] public ViewResult LocalIndex() { return View("Result", new Result { ControllerName = "Customer", ActionName = "LocalIndex" }); } ... }

10、Excute 方法是(自定义或默认的)ActionInvoker 的入口函数。ActionInvoker 必须实现 IActionInvoker 接口来查找和调用 Action 方法。本文没有介绍 MvcHandler 的知识。MvcHandler 是处理Controller的开始,但在MvcHandler 之前还有一个MvcRouteHandler,当请求经过路由解析后,MvcRouteHandler 实例会生成一个 MvcHandler 实例,并把请求交给它。MvcHandler 会从Controller 工厂中获取一个 Controller 实例,然后由 Controller 来具体地处理请求。

转载于:https://www.cnblogs.com/wangwust/p/6387502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值