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

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