ActionResult source code analysis notes

ActionResult is an abstract class:

    public abstract class ActionResult
    {
        public abstract void ExecuteResult(ControllerContext context);
    }

ActionResult classes are achieved by operating the Response object, to achieve different output

 

ActionResult implementation class:

JsonResult:

Get prohibit access by default, so the need to add AllowGet Get request parameter  

response.ContentType = "application/json";
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.Serialize(Data)

ContentResult:

response.Write(Content);

EmptyResult:

Return nothing ~

RedirectResult:

context.HttpContext.Response.Redirect(url, false);

NotFound:

context.HttpContext.Response.StatusCode = 404;

HttpStatusCodeResult:同上

HttpUnauthorizedResult: Unauthorized, ibid.

FileResult: files, overloaded

// byte array 
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

Get Class name of the view according to the view, view class is a subclass of WebViewPage class, instantiated and variable transmission (ViewData, Model ..), call the view class RenderView () method writes the content output stream

Guess you like

Origin www.cnblogs.com/fanfan-90/p/12079030.html