编程语言
首页 > 编程语言> > ActionResult源码分析笔记

ActionResult源码分析笔记

作者:互联网

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

 

PartialViewResult

标签:num,ActionResult,笔记,response,Write,源码,context,Response
来源: https://www.cnblogs.com/fanfan-90/p/12079030.html