Controller in ASP.NET Core

Before the advent of ASP.NET CORE, the Controller we implemented, MVC inherited from the Controller base class, WebApi, inherited from ApiController. Now ASP.NET CORE has merged MVC with WebApi and no longer distinguishes between MVC or WebApi. The inheritance structure of the Controller of ASP.NET CORE has also changed. When we look at other examples, we will find that some inherit from Controller and some inherit from ControllerBase. In fact, ControllerBase is the base class of Controller. In other words, if you inherit from Controller, you actually inherit ControllerBase. When should I choose to inherit ControllerBase directly?

ControllerBase

Let's first look at the metadata of ControllerBase:

it is very long and not complete. You can see that ControllerBase is an abstract class and implements a large number of virtual methods. Most of these virtual methods correspond to Http status codes.
such as:

public virtual OkResult Ok(); //http status 200
public virtual NotFoundResult NotFound(); //http status 404
public virtual ForbidResult Forbid(); //http status 403
public virtual CreatedResult Created(Uri uri, [ActionResultObjectValue] object value); // http status 201
...还有很多很多...

Obviously this is the base class designed for Restful Api, so when you want to design a Restful (web api) interface, you can choose to inherit from ControllerBase, it can already meet your needs.

Controller

Look at the metadata of the Controller: The

Controller is also an abstract class, inherited from ControllerBase, and inherits several interfaces. Obviously, the content of Controller more than ControllerBase is mainly something that deals with MVC.
For example: Viewbag, Viewdata properties, Json, View methods, etc .:

public dynamic ViewBag { get; }
public ViewDataDictionary ViewData { get; set; }
public virtual JsonResult Json(object data);
public virtual ViewResult View();
...

So if you need to implement an MVC system, if you want to use cshtml template and razor to try engine rendering page, you need to inherit Controller.

LITTLE Controller

In addition to inheriting Controller and ControllerBase, the ASP.NET CORE framework can make your POCO class directly become a Controller.

Use "Controller" suffix

Can the TestController work properly with the following code?

    [Route("api/[controller]")]
    public class TestController 
    {
        [HttpGet]
        public string Get()
        {
            return "TestController";
        }
    }

Run it:

Although the TestController class does not inherit from any class, but it does work in the ASP.NET CORE framework. The ASP.NET CORE framework will find the class with the suffix "Controller" by default and use it as a real Controller. It will also be tried to match when the routing system finally matches the Controller.

Use ControllerAttribute

If your controller class has any special requirements, and even the class name does not want to add the "Controller" suffix, then another method is to use ControllerAttribute.

    [Controller]
    [Route("api/[controller]")]
    public class POCO 
    {
        [HttpGet]
        public string Get()
        {
            return "POCOController";
        }
    }

Run it: The

POCO class does not inherit from any class, and there is no "Controller" suffix name, but because it is marked ControllerAttribute will also be considered a Controller by the ASP.NET CORE framework. It will also be tried to match when the routing system finally matches the Controller.

Use NonControllerAttribute

If one of your class names happens to include the "Controller" suffix, but you don't want the ASP.NET CORE framework to find it, you can add NonControllerAttribute to the class. So ASP.NET CORE framework will ignore it.
Change the TestController just now, plus [NonController]:

    [NonController]
    [Route("api/[controller]")]
    public class TestController 
    {
        [HttpGet]
        public string Get()
        {
            return "TestController";
        }
    }

Run it:

/ api / test no longer matches the controller.

to sum up

  1. ControllerBase can be inherited when designing restful (web api) interface
  2. Controller can be inherited when designing MVC system
  3. When a POCO class name contains "Controller" suffix or when adding ControllerAttribute, the framework will consider it as a controller
  4. NonControllerAttribute can be added when a class does not want to be used as a controller by the framework

Guess you like

Origin www.cnblogs.com/kklldog/p/aspnetcore-controller.html