AspNetCore 2.1 中的Controller 总结

概述

在AspNetCore 项目中,我们首先使用的类就是Controller,Controller表示MVC中的C,用于协调M(Model)和V(View)。
我们在使用VS创建Web Api或 Web Mvc模板项目时,微软提供了几个Controller,如ControllerBase,Controller 和ApiController,在使用时有必要了解其关系。

ControllerBase

ControllerBase顾名思义,表示控制器基类,微软在.NetCore 时代统一了Controller类型,无论WebApi项目还是Mvc项目,都以此类作为控制器基类。

NameSpace:  Microsoft.AspNetCore.Mvc
所属DLL:Microsoft.AspNetCore.Mvc.Core.dll
//
// Summary:
//     A base class for an MVC controller without view support.
[Controller]
public abstract class ControllerBase
{
   ...
}

从源码元数据可以看出,ControllerBase是一个标记了ControllerAtribute 的抽象类,不支持view,即表示不能用于带页面控制的MVC项目。

Controller

Controller 类是在MVC web中出现的控制器基类,有如下描述。

NameSpace:Microsoft.AspNetCore.Mvc
DLL:Microsoft.AspNetCore.Mvc.ViewFeatures.dll
//
// Summary:
//     A base class for an MVC controller with view support.
public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
{
        
}

可以看出Controller 派生自ControllerBase,来自于同一个命名空间,且Controller 类是用于Web MVC项目,丰富了很多的功能,扩展了对视图的支持。

ControllerAttribute

ControllerAttribute 是标记在ControllerBase是上面的特性,有如下描述:

namespace Microsoft.AspNetCore.Mvc
{
    //
    // Summary:
    //     Indicates that the type and any derived types that this attribute is applied
    //     to are considered a controller by the default controller discovery mechanism,
    //     unless Microsoft.AspNetCore.Mvc.NonControllerAttribute is applied to any type
    //     in the hierarchy.
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class ControllerAttribute : Attribute
    {
        public ControllerAttribute();
    }
}

这个特性总体来说用于mvc路由发现和控制器激活机制,可以用于将自定义的class 用作mvc 控制器,比如你如果不想遵循Controller作为类文件后缀的约定,可以很方便的修改。
如下代码中,Bar将用做Controller。

[Controller]
public class Endpoint {}
 
[Route("api/[controller]")]
public class Bar : Endpoint
{
    [HttpGet]
    public string Get()
    {
        return "bar";
    }
}

与这个特性相对的是【NonController】,这个特性表示被标记的类不再作为控制器。

namespace Microsoft.AspNetCore.Mvc
{
    //
    // Summary:
    //     Indicates that the type and any derived types that this attribute is applied
    //     to is not considered a controller by the default controller discovery mechanism.
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class NonControllerAttribute : Attribute
    {
        public NonControllerAttribute();
    }
}

【NonController】比Controller 有更高的优先级,如果这个特性出现在类层次结构的任何位置,则表示改类和其子类都不再被mvc框架视为控制器。

ApiControllerAttribute

【ApiController】 特性用于webapi项目的控制器上,有如下描述:

namespace Microsoft.AspNetCore.Mvc
{
    //
    // Summary:
    //     Indicates that a type and all derived types are used to serve HTTP API responses.
    //     The presence of this attribute can be used to target conventions, filters and
    //     other behaviors based on the purpose of the controller.
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata, IFilterMetadata
    {
        public ApiControllerAttribute();
    }
}

从元数据描述可以看出,ApiControllerAttribute 派生自ControllerAttribute,这个特性加上之后,为开发Api接口提供了很多便利,比如Action参数的序列化和反序列化,比如模型验证,主要通过IApiBehaviorMetadata 接口实现。

https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章