Ok() 方法 new ObjectResult() 有什么区别吗?

Is there any difference between the Ok() method new ObjectResult()?(Ok() 方法 new ObjectResult() 有什么区别吗?)
本文介绍了Ok() 方法 new ObjectResult() 有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:在 .net 核心控制器上实现标准 REST API/GET 方法.

Scenario: implementing a standard REST API / GET method on a .net core controller.

文档指出 OkObjectResult是一个状态为 200 的 ObjectResult.这可通过从 ControllerBase 继承的 Ok(myResult) 方法获得.我认为这是一种方便的方法.

The documentation states that OkObjectResult is an ObjectResult with status 200. This is available through the Ok(myResult) method inherited from ControllerBase. I assume this is a convenience method.

但是,教程没有使用这种方法 - 它改为返回 new ObjectResult(myResult) ,默认状态为 200.

However, the tutorial is not using this approach - it instead returns new ObjectResult(myResult) which will default to status 200.

这两种方法有什么区别吗?

Is there any difference between these two approaches?

推荐答案

技术上这两种方法没有区别.

如果你想看OkObjectResult 那么你会看到 OkObjectResult 是一个 ObjectResult 设置200状态码,这是默认的ObjectResult 已经.

If you want to look at the code of OkObjectResult then you will see that the OkObjectResult is an ObjectResult that sets the 200 status code, which is the default of ObjectResult already.

对我而言,唯一的区别是代码的可读性以及您自己或您的团队的偏好.这完全取决于命名以及您要强调的意图.

The only difference for me is readability in code and your own or your team preferences. It is all about naming and what intention you want to stress.

 return Ok(myResult);                  // gives emphasis on status, my personal favorite

 return new OkObjectResult(myResult);  // for me a little bit verbose and the same
                                       // effect as Ok; but states we return an Object

 return new ObjectResult(myResult);    // gives emphasis of the content that is returned

这篇关于Ok() 方法 new ObjectResult() 有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)