Asp.Net MVC 响应结果IActionResult分析

Asp.Net Core 响应结果中默认返回值为IActionResult,我们可以再Action返回简单字符串、实体对象、视图等多种数据,这些结果都被封装在IActionResult中,对于不同的返回结果 mvc框架提供了多种类型结果实现了此接口。

下表中列出了不同返回类型的返回结果

动作类型行为结果
EmptyResult如果Action方法返回类型为void,或者返回值为Null,最终生成的就是一个EmptyResult对象
ContentResultContent(string)返回一串字符串
FileContentResult : FileResultFile(byte[] fileContents, string contentType, string fileDownloadName)由于FileContentResult是根据字节数组创建的,当我们需要动态生成响应文件内容(而不是从物理文件中读取)时,FileContentResult是一个不错的选择
FilePathResult : FileResultFile(string fileName, string contentType, string fileDownloadName)响应文件的路径通过只读属性FileName表示,该属性在构造函数中被初始化。在实现的WriteFile方法中,FilePathResult直接将文件路径作为参数调用当前HttpResponse的TransmitFile实现了针对文件内容的响应
FileStreamResult : FileResultFile(Stream fileStream, string contentType, string fileDownloadName)在实现的WriteFile方法中,FileStreamResult通过指定的文件流读取文件内容,并最终调用当前HttpResponse的OutputStream属性的Write方法将读取的内容写入当前HTTP响应的输出流中
JsonResultJson(Object)返回Json格式数据
RedirectToResultRedirect(Url) RedirectToAction(“Index”, “Home”, new { id = “100”, name = “liu” }); / RedirectPermanent(Url)Redirect/RedirectPermanent方法用于创建重定向到指定URL的RedirectResult(302:301) ; RedirectToAction/RedirectToActionPermanent用于创建重定向到指定的目标Action的RedirectResult/RedirectToRouteResult
RedirectToRouteResultRedirectToRoute/RedirectToRoutePermanen RedirectToRoute(“Default”, new { controller = “Home”, action = “Index”});RedirectToRoute / RedirectToRoutePermanent创建的RedirectResult/RedirectToRouteResult对象是针对注册的某个路由的
ViewResultView()接收视图引擎的响应
PartialViewResultView()接收分部视图引擎的响应
HttpUnauthorizedResult抛出401错误
JavaScriptResult返回javascript文件

模型对象等,使用产生ActionResult的对象的典型优点就是它以正式的方式封装控制器决策,控制器不必直接在响应中写入其决策结果,只需要返回决策,框架本身获取结果并理解如何将结果进行何种转换以便可以通过HTTP进行发回。
图1:

在这里插入图片描述图2:在这里插入图片描述以上不一定core支持,没有验证完

参考文章:
https://www.cnblogs.com/artech/archive/2012/08/13/action-result-01.html
https://blog.csdn.net/lomeo23/article/details/52021603
https://blog.csdn.net/pasic/article/details/7110134
https://blog.csdn.net/ken_io/article/details/85711636

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值