首页 \ 问答 \ 构建API响应(Structuring an API response)

构建API响应(Structuring an API response)

我不确定从API构建响应的最佳方法。

每个响应都应该有“结果”或“成功”节点吗?

我在考虑以下模板:

   -result (true/false)
   -data (only if result is true)
   -errors (only if result is false)

这是一种公认​​的做法,还是有更标准的做事方式?


I'm not sure on the best way to structure the response from our API.

Should every response have a 'result' or 'success' node?

I'm thinking of the following template:

   -result (true/false)
   -data (only if result is true)
   -errors (only if result is false)

Is this accepted practice, or is there a more standard way of doing things?


原文:https://stackoverflow.com/questions/12424251
更新时间:2023-10-08 20:10

最满意答案

忘掉ASP.NET MVC中的服务器端控件。 包含runat="server"内容在ASP.NET MVC中都是很大的NO 。 无论如何,在剃刀中都无法使用。 没有优点和缺点。 永远不要使用服务器端控件,因此无需比较。 只需使用HTML助手,如Html.EditorForHtml.TextBoxFor


Forget about server side controls in ASP.NET MVC. Everything containing runat="server" is a big NO in ASP.NET MVC. Won't work in Razor anyways. There are no pros and cons. Server side controls should never be used so there's nothing to compare. Simply use HTML helpers such as Html.EditorFor and Html.TextBoxFor.

相关问答

更多
  • 我想你应该这样使用 @Html.TextBoxFor(model => model.Name, new { @class = "text", type = "email", required = "required" }) 我认为这会帮助你。 i think you should use like this @Html.TextBoxFor(model => model.Name, new { @class = "text", type = "email", required = "required" ...
  • 请试试这个 $(document).ready(function(){ $("select").change(function(){ $('input').attr('maxlength',$(this).val()); }); }); DEMO Please try this $(document).ready(function(){ $("select").change(function(){ $('input').attr('maxlength ...
  • 您似乎依赖于IE的旧怪癖,其中元素名称和ID被添加为全局变量。 依靠它是不好的做法,因为其他浏览器不支持它(从一开始就是一个非常糟糕的主意)。 正确引用表单元素,它应该工作。 例如 function rblToolType_OnChange(isUnlock) { var form = document.forms['
    '] if (isUnlock) { form.Password.style.display = "non ...
  • 我的理解是你的 this.href == localhost:3325/WebProduct/Add?name=Panadol&type=Tablet 如果这是正确的,那么以下应该有效 $(function () { $('#edit').click(function () { var name = $('#quantity').val(); var href = this.href; var splitHalfUrl = href. ...
  • HTML.Textbox: 使用指定的HTML帮助器和表单字段的名称返回文本输入元素。 HTML.Editor: 此方法根据正在呈现的属性的数据类型以及根据属性是否标记了某些属性来生成不同的HTML标记。 如果该属性被键入为基本类型(整数,字符串等),则该方法为文本框呈现HTML输入元素。 如果属性标记为数据类型属性或UIHintAttribute属性,则该属性将指定为该属性生成的标记。 例如,如果该属性标有MultilineText属性,则该方法会为多行文本框生成标记。 如果对象包含多个属性,则对于每个属 ...
  • 你可以从中得到它 Request.Form 但这是非常原始的解决方案。 您必须使用强类型视图,并在操作中处理模型。 You can get it from the Request.Form But this is very primitive solution. You must use strongly typed views and work with your model in actions.
  • 您正在调用该方法的string name, object value重载 ,因此第二个参数将被视为value ,而不是htmlAttributes 。 您应该使用方法的不同重载,可能是string name, object value, object htmlAttributes是指定一个空value : @Html.TextBox("term", "", new { placeholder = "What are you searching for?" }) You're calling the str ...
  • 继Charles / Charlino所说: 模型绑定更新ModelState对象,该对象包含在模型绑定期间收集的验证和模型绑定错误。 在操作方法内部,已经发生了模型绑定以更新模型,并生成了ModelState对象。 如果现在更新操作内部模型的值,则还必须手动更新模型状态(因为帮助程序使用它来生成HTML)。 以下是一个例子: model.CaptchaIsValid = CaptchaService.ValidateAndExpireCaptcha(model.CaptchaAttempt); if (! ...
  • 忘掉ASP.NET MVC中的服务器端控件。 包含runat="server"内容在ASP.NET MVC中都是很大的NO 。 无论如何,在剃刀中都无法使用。 没有优点和缺点。 永远不要使用服务器端控件,因此无需比较。 只需使用HTML助手,如Html.EditorFor和Html.TextBoxFor 。 Forget about server side controls in ASP.NET MVC. Everything containing runat="server" is a big NO in ...
  • 为什么此输入字段添加了data- *属性? 因为您已在应用程序中启用了不显眼的验证。 默认情况下,MVC验证系统允许对值类型(整数,日期时间)的必需和数据类型进行客户端验证。 为什么它甚至认为这是约会? 当您将字符串传递给Html.TextBox帮助程序方法时,它会检查Model包含具有该名称的任何属性,在您的情况下, Model具有名称为EndTime且属于DateTime的属性。 您正在传递仍表示类型DateTime EndTime.Date ( DateTime具有属性Date ,其类型为DateTi ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)