![](/img/trans.png)
[英]Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller
[英]ASP.NET Core 2.2 Web API controller inherited from Object class (not from Controller class)
我在一个 Web ZDB974238714CA8DE634A7CE1DZ 项目中看到了 controller class 的以下声明
[Route("api/[controller]")]
public class UrlController
{
private readonly IConfiguration configuration;
public UrlController(IConfiguration configuration)
{
this.configuration = configuration;
}
[HttpGet]
[ProducesResponseType(typeof(string), 200)]
public string Get() => this.configuration.GetSection("ShortUrlPrefix").Get<string>();
}
我认为控制器必须从 Controller/ControllerBase 继承,但在这种情况下,它可以在没有 inheritance 的情况下工作。 这个特定的声明有什么问题吗? 如果我们只需要返回一个简单的值(在这种情况下是一个短字符串),它是一种“优化”吗?
这个特定的声明有什么问题吗?
没有问题。
控制器通常继承自 Controller,尽管这不是必需的。
如果您需要更复杂的场景并提供许多可用于处理 HTTP 请求的属性和方法,则可以使用基础 controller 类。
如果我们只需要返回一个简单的值(在这种情况下是一个短字符串),它是一种“优化”吗?
这是设计使然。 它遵循框架定义的约定
controller 是可实例化的 class,其中至少满足以下条件之一:
- class 名称后缀为“Controller”
- class 继承自名称以“Controller”为后缀的 class
- class 装饰有 [Controller] 属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.