ActionResult是一个抽象类:
public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); }
ActionResult的实现类都是通过操作Response对象,实现不同输出
ActionResult的实现类:
JsonResult:
默认情况下禁止Get访问,所以Get请求需要加AllowGet参数
response.ContentType = "application/json"; JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.Serialize(Data)
ContentResult:
response.Write(Content);
EmptyResult:
什么都不返回~
RedirectResult:
context.HttpContext.Response.Redirect(url, false);
NotFound:
context.HttpContext.Response.StatusCode = 404;
HttpStatusCodeResult:同上
HttpUnauthorizedResult:未授权,同上
FileResult:文件,重载
//字节数组
response.OutputStream.Write(FileContents, 0, FileContents.Length);
//流 Stream outputStream = response.OutputStream; using (FileStream) { byte[] buffer = new byte[4096]; while (true) { int num = FileStream.Read(buffer, 0, 4096); if (num != 0) { outputStream.Write(buffer, 0, num); continue; } break; } }
JavaScriptResult:
HttpResponseBase response = context.HttpContext.Response; response.ContentType = "application/x-javascript"; if (Script != null) { response.Write(Script); }
ViewResult
根据视图名称获取视图类,视图类是WebViewPage类的子类,实例化并传变量(ViewData、Model..),调用视图类的RenderView()方法,将内容写入output流中
原文:https://www.cnblogs.com/fanfan-90/p/12079030.html