当前位置: 首页 > news >正文 西宁网站建设官网一个好的营销型网站模板 news 2025/11/1 2:38:44 西宁网站建设官网,一个好的营销型网站模板,教育培训班,深圳专门网站制作ASP.NET Core为Web API控制器方法返回类型提供了如下几个选择#xff1a; Specific type IActionResult ActionResultT 1. 返回指定类型#xff08;Specific type#xff09; 最简单的API会返回原生的或者复杂的数据类型#xff08;比如#xff0c;string 或者… ASP.NET Core为Web API控制器方法返回类型提供了如下几个选择 Specific type IActionResult ActionResultT 1. 返回指定类型Specific type 最简单的API会返回原生的或者复杂的数据类型比如string 或者自定义对象类型。考虑如下的Action方法其返回了一个自定义的Author对象的集合。 [HttpGet] public ListAuthor Get() _repository.GetAuthors(); [HttpGet] public IEnumerableAuthor Get() { return _repository.GetAuthors(); } 从 NetCore 3.0 开始你不仅可以定义同步形式的 IEnumerableAuthor方法也可以定义异步形式的 IAsyncEnumerableT方法不同点在于后者是一个异步模式的集合好处就是不会导致同步迭代既不阻塞数据库也不阻塞主线程。 下面的代码展示了如何改造 Get 方法两个方法返回都是不阻塞的 [HttpGet] public IEnumerableAuthor Get() { var authors _repository.GetAuthors();// 需要等待authors全部查询完成才会进入下一步迭代但是返回时又做了一个判断筛选返回是不阻塞的 foreach (var author in authors) // 适合数据库量大时只做全部查询然后通过foreach筛选yield迭代返回 { if(author.isMale){ yield return author; } } } [HttpGet] public async IAsyncEnumerableAuthor Get() //异步迭代既不会阻塞数据库也不阻塞主线程 { var authors GetAuthorsAsync(); // 使用GetAuthorsAsync异步方法不用authors查询完毕就会进入下一步迭代返回authors await foreach (var author in authors) { yield return author; } } 2. 返回 IActionResult 实例 如果你要返回 data httpcode 的双重需求那么 IActionResult 就是你要找的东西下面的代码片段展示了如何去实现。 [HttpGet] public IActionResult Get() { if (authors null) return NotFound(No records); return Ok(authors); // 必须有Ok等方法包装 } 上面的代码有 OkNotFound 两个方法对应着 OKResultNotFoundResult Http Code 对应着 200404。当然还有其他的如CreatedResult, NoContentResult, BadRequestResult, UnauthorizedResult, 和 UnsupportedMediaTypeResult都是 IActionResult 的子类。 3. 返回 ActionResultT 实例 ActionResultT包装了前面这种模式可以返回 IActionResultdata httpcode也可以返回指定类型T [HttpGet] public ActionResultIEnumerableAuthor Get() { if (authors null) return NotFound(No records); return authors; } 和之前IActionResult的 Get 方法相比这里如果不需要返回httpCode则直接返回 authors 而不需要再用 OK(authors) 包装是一个非常好的简化而IActionResult必须要使用Ok等包装一下返回。 接下来再把 Get 方法异步化 [HttpGet] public async TaskActionResultIEnumerableAuthor Get() { var data await GetAuthors(); if (data null) return NotFound(No record); return data; } 如果你有一些定制化需求可以实现一个自定义的 ActionResult 类做法就是实现 IActionResult 中的 ExecuteResultAsync 方法即可。 如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。 文章转载自: http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn 查看全文 http://www.tj-hxxt.cn/news/266855.html 相关文章: 织梦网站图片怎么修改网络推广培训推荐 jsp做的婚恋网站wordpress始终无法登录 东莞市住建局官网网站wordpress 附件 标签 设置网站字体wordpress图片 外链 如何使用微信公众号做网站怎么建设电影网站 电商网站设计内容自己在线制作logo免费下载 上海哪家做网站好公司建网站找哪家 捕鱼游戏网站建设步骤sketchup模型库免费下载 有口碑的唐山网站建设松江新城投资建设集团发展有限公司网站 如何在局域网建立网站wordpress自定义应用 网站制作还花钱百度软件推广联盟 四川省城市建设培训中心 网站山西省建设厅招标网站 网站建设中 动态图片网站优化网站建站教程 成立网站有什么要求百度知道灰色词代发收录 制作简历模板网站o2o电子商务模式的特点 网站开发和桌面开发哪个难江门cms模板建站 苏州企业网站建设推广公司产品文案该怎么写 公司备案证查询网站荣耀官方网站 土木特网站建设安贞做网站公司 国外优秀app设计网站中国乐清新闻 如何在一个地方建设网站深圳建站公司外围 python网站开发 pdf定服装网站建设 自己切片视频做网站wordpress页面移动端 做医疗器械网站京东购物商城 12380网站建设情况汇报零基础学python要多久 浏览器正能量网站免费微信公众号小程序 网站搭建中114514甘肃网站建设专家 常熟智能网站建设大连招标网 苏州网站工作室佛山市住房建设局网站办事大厅 网站商城怎么做网络设计师证怎么考