zoukankan      html  css  js  c++  java
  • ASP.NET MVC ActionResult的实现

    1、11种ActionResult

    在System.Web.Mvc命名空间下:

    ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 版本中提供的ActionResult派生类:

    类名 抽象类 父类 功能
    ContentResult     根据内容的类型和编码,数据内容.
    EmptyResult     空方法.
    FileResult abstract   写入文件内容,具体的写入方式在派生类中.
    FileContentResult   FileResult 通过 文件byte[] 写入文件.
    FilePathResult   FileResult 通过 文件路径 写入文件.
    FileStreamResult   FileResult 通过 文件Stream 写入文件.
    HttpUnauthorizedResult     抛出401错误
    JavaScriptResult     返回javascript文件
    JsonResult     返回Json格式的数据
    RedirectResult     使用Response.Redirect重定向页面
    RedirectToRouteResult     根据Route规则重定向页面
    ViewResultBase abstract   调用IView.Render()
    PartialViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 
    重写了父类的FindView方法. 
    寻找用户控件.ascx文件
    ViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 
    重写了父类的FindView方法. 
    寻找页面.aspx文件

    2、代码

     1 public class ActionResultController : Controller
     2     {
     3         public ActionResult Index()
     4         {
     5             return View();
     6         }
     7         public ActionResult ContentResult()
     8         {
     9             return Content("Hi, 我是ContentResult结果");
    10         }
    11         public ActionResult EmptyResult()
    12         {
    13             //空结果当然是空白了!
    14             //至于你信不信, 我反正信了
    15             return new EmptyResult();
    16         }
    17         public ActionResult FileResult()
    18         {
    19             var imgPath = Server.MapPath("~/demo.jpg");
    20             return File(imgPath, "application/x-jpg", "demo.jpg");
    21         }
    22         public ActionResult HttpNotFoundResult()
    23         {
    24             return HttpNotFound("Page Not Found");
    25         }
    26         public ActionResult HttpUnauthorizedResult()
    27         {
    28             //未验证时,跳转到Logon
    29             return new HttpUnauthorizedResult();
    30         }
    31         public ActionResult JavaScriptResult()
    32         {
    33             string js = "alert("Hi, I'm JavaScript.");";
    34             return JavaScript(js);
    35         }
    36         public ActionResult JsonResult()
    37         {
    38             var jsonObj = new
    39             {
    40                 Id = 1,
    41                 Name = "小铭",
    42                 Sex = "男",
    43                 Like = "足球"
    44             };
    45             return Json(jsonObj, JsonRequestBehavior.AllowGet);
    46         }
    47         public ActionResult RedirectResult()
    48         {
    49             return Redirect("~/demo.jpg");
    50         }
    51         public ActionResult RedirectToRouteResult()
    52         {
    53             return RedirectToRoute(new {
    54                 controller = "Hello", action = ""
    55             });
    56         }
    57         public ActionResult ViewResult()
    58         {
    59             return View();
    60         }
    61         public ActionResult PartialViewResult()
    62         {
    63             return PartialView();
    64         }
    65         //禁止直接访问的ChildAction
    66         [ChildActionOnly]
    67         public ActionResult ChildAction()
    68         {
    69             return PartialView();
    70         }
    71         //正确使用ChildAction
    72         public ActionResult UsingChildAction()
    73         {
    74             return View();
    75         }
    76     }
  • 相关阅读:
    想算法无头绪,咋办?
    使用R语言分析股价波动
    cmd控制台 wrapper | OpenSCManager failed
    Ubuntu使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母解决方案
    Ubuntu su命令 Authentication failure的解决办法
    Springboot2.x整合RabbitMQ
    RabbitMQ 笔记
    Ubuntu安装RabbitMQ
    使用xshell连接ubuntu
    Java 泛型
  • 原文地址:https://www.cnblogs.com/webapi/p/5669037.html
Copyright © 2011-2022 走看看