asp.net core控制器方法返回输出

转载:ASP.NET Core Web API 控制器与方法返回输出

DATA ACCESS LAYER

在一些不同的示例教程中,我们可能看到 DAL 的实现在主项目中,并且每个控制器中都有实例。我们不建议这么做。

当我们编写 DAL 时,我们应该将其作为一个独立的服务来创建。在 .NET Core 项目中,这一点很重要,因为当我们将 DAL 作为一个独立的服务时,我们就可以将其直接注入到 IOC(控制反转)容器中。IOC 是 .NET Core 内置功能。通过这种方式,我们可以在任何控制器中通过构造函数注入的方式来使用。

1

2

3

4

5

6

7

8

publicclassOwnerController: Controller

{

privateIRepository _repository;

publicOwnerController(IRepository repository)

{

_repository = repository;

}

}

  

CONTROLLERS

控制器应该始终尽量保持整洁。我们不应该将任何业务逻辑放置于内。

因此,我们的控制器应该通过构造函数注入的方式接收服务实例,并组织 HTTP 的操作方法(GET,POST,PUT,DELETE,PATCH...):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

publicclassOwnerController : Controller

{

privatereadonlyILoggerManager _logger;

privatereadonlyIRepository _repository;

publicOwnerController(ILoggerManager logger, IRepository repository)

{

_logger = logger;

_repository = repository;

}

[HttpGet]

publicIActionResult GetAllOwners()

{

}

[HttpGet("{id}", Name ="OwnerById")]

publicIActionResult GetOwnerById(Guid id)

{

}

[HttpGet("{id}/account")]

publicIActionResult GetOwnerWithDetails(Guid id)

{

}

[HttpPost]

publicIActionResult CreateOwner([FromBody]Owner owner)

{

}

[HttpPut("{id}")]

publicIActionResult UpdateOwner(Guid id, [FromBody]Owner owner)

{

}

[HttpDelete("{id}")]

publicIActionResult DeleteOwner(Guid id)

{

}

}

  

我们的 Action 应该尽量保持简洁,它们的职责应该包括处理 HTTP 请求,验证模型,捕捉异常和返回响应。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[HttpPost]

publicIActionResult CreateOwner([FromBody]Owner owner)

{

try

{

if(owner.IsObjectNull())

{

returnBadRequest("Owner object is null");

}

if(!ModelState.IsValid)

{

returnBadRequest("Invalid model object");

}

_repository.Owner.CreateOwner(owner);

returnCreatedAtRoute("OwnerById",new{ id = owner.Id }, owner);

}

catch(Exception ex)

{

_logger.LogError($"Something went wrong inside the CreateOwner action: { ex} ");

returnStatusCode(500,"Internal server error");

}

}

  

在大多数情况下,我们的 action 应该将 IActonResult 作为返回类型(有时我们希望返回一个特定类型或者是 JsonResult ...)。通过使用这种方式,我们可以很好地使用 .NET Core 中内置方法的返回值和状态码。

使用最多的方法是:

    • OK => returns the 200 status code

    • NotFound => returns the 404 status code

    • BadRequest => returns the 400 status code

    • NoContent => returns the 204 status code

    • Created, CreatedAtRoute, CreatedAtAction => returns the 201 status code

    • Unauthorized => returns the 401 status code

    • Forbid => returns the 403 status code

    • StatusCode => returns the status code we provide as input

DATA ACCESS LAYER

在一些不同的示例教程中,我们可能看到 DAL 的实现在主项目中,并且每个控制器中都有实例。我们不建议这么做。

当我们编写 DAL 时,我们应该将其作为一个独立的服务来创建。在 .NET Core 项目中,这一点很重要,因为当我们将 DAL 作为一个独立的服务时,我们就可以将其直接注入到 IOC(控制反转)容器中。IOC 是 .NET Core 内置功能。通过这种方式,我们可以在任何控制器中通过构造函数注入的方式来使用。

1

2

3

4

5

6

7

8

publicclassOwnerController: Controller

{

privateIRepository _repository;

publicOwnerController(IRepository repository)

{

_repository = repository;

}

}

  

CONTROLLERS

控制器应该始终尽量保持整洁。我们不应该将任何业务逻辑放置于内。

因此,我们的控制器应该通过构造函数注入的方式接收服务实例,并组织 HTTP 的操作方法(GET,POST,PUT,DELETE,PATCH...):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

publicclassOwnerController : Controller

{

privatereadonlyILoggerManager _logger;

privatereadonlyIRepository _repository;

publicOwnerController(ILoggerManager logger, IRepository repository)

{

_logger = logger;

_repository = repository;

}

[HttpGet]

publicIActionResult GetAllOwners()

{

}

[HttpGet("{id}", Name ="OwnerById")]

publicIActionResult GetOwnerById(Guid id)

{

}

[HttpGet("{id}/account")]

publicIActionResult GetOwnerWithDetails(Guid id)

{

}

[HttpPost]

publicIActionResult CreateOwner([FromBody]Owner owner)

{

}

[HttpPut("{id}")]

publicIActionResult UpdateOwner(Guid id, [FromBody]Owner owner)

{

}

[HttpDelete("{id}")]

publicIActionResult DeleteOwner(Guid id)

{

}

}

  

我们的 Action 应该尽量保持简洁,它们的职责应该包括处理 HTTP 请求,验证模型,捕捉异常和返回响应。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[HttpPost]

publicIActionResult CreateOwner([FromBody]Owner owner)

{

try

{

if(owner.IsObjectNull())

{

returnBadRequest("Owner object is null");

}

if(!ModelState.IsValid)

{

returnBadRequest("Invalid model object");

}

_repository.Owner.CreateOwner(owner);

returnCreatedAtRoute("OwnerById",new{ id = owner.Id }, owner);

}

catch(Exception ex)

{

_logger.LogError($"Something went wrong inside the CreateOwner action: { ex} ");

returnStatusCode(500,"Internal server error");

}

}

  

在大多数情况下,我们的 action 应该将 IActonResult 作为返回类型(有时我们希望返回一个特定类型或者是 JsonResult ...)。通过使用这种方式,我们可以很好地使用 .NET Core 中内置方法的返回值和状态码。

使用最多的方法是:

    • OK => returns the 200 status code

    • NotFound => returns the 404 status code

    • BadRequest => returns the 400 status code

    • NoContent => returns the 204 status code

    • Created, CreatedAtRoute, CreatedAtAction => returns the 201 status code

    • Unauthorized => returns the 401 status code

    • Forbid => returns the 403 status code

    • StatusCode => returns the status code we provide as input