MVC学习三:MVC Action Result 返回类型实例

MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中,其类型有:

  
  1. ContentResult   
  2.   
  3.   EmptyResult   
  4.   
  5.   FileResult   
  6.   
  7.   HttpStatusCodeResult   
  8.   
  9.   HttpNotFoundResult   
  10.   
  11.   HttpUnauthorizedResult   
  12.   
  13.   JavaScriptResult   
  14.   
  15.   JsonResult   
  16.   
  17.   RedirectResult   
  18.   
  19.   RedirectToRouteResult   
  20.   
  21.   ViewResultBase   
  22.   
  23.   PartialViewResult   
  24.   
  25.   ViewResult  

  示例代码:   

  1. public class ActionResultController : Controller   
  2.   
  3.   {   
  4.   
  5.   public ActionResult Index()   
  6.   
  7.   {   
  8.   
  9.   return View();   
  10.   
  11.   }   
  12.   
  13.   public ActionResult ContentResult()   
  14.   
  15.   {   
  16.   
  17.   return Content("Hi, 我是ContentResult结果");   
  18.   
  19.   }   
  20.   
  21.   public ActionResult EmptyResult()   
  22.   
  23.   {   
  24.   
  25.   //空结果当然是空白了!   
  26.   
  27.   //至于你信不信, 我反正信了   
  28.   
  29.   return new EmptyResult();   
  30.   
  31.   }   
  32.   
  33.   public ActionResult FileResult()   
  34.   
  35.   {   
  36.   
  37.   var imgPath = Server.MapPath("~/demo.jpg");   
  38.   
  39.   return File(imgPath, "application/x-jpg", "demo.jpg");   
  40.   
  41.   }   
  42.   
  43.   public ActionResult HttpNotFoundResult()   
  44.   
  45.   {   
  46.   
  47.   return HttpNotFound("Page Not Found");   
  48.   
  49.   }   
  50.   
  51.   public ActionResult HttpUnauthorizedResult()   
  52.   
  53.   {   
  54.   
  55.   //未验证时,跳转到Logon   
  56.   
  57.   return new HttpUnauthorizedResult();   
  58.   
  59.   }   
  60.   
  61.   public ActionResult JavaScriptResult()   
  62.   
  63.   {   
  64.   
  65.   string js = "alert(\"Hi, I'm JavaScript.\");";   
  66.   
  67.   return JavaScript(js);   
  68.   
  69.   }   
  70.   
  71.   public ActionResult JsonResult()   
  72.   
  73.   {   
  74.   
  75.   var jsonObj = new  
  76.   
  77.   {   
  78.   
  79.   Id = 1,   
  80.   
  81.   Name = "小铭",   
  82.   
  83.   Sex = "男",   
  84.   
  85.   Like = "足球"  
  86.   
  87.   };   
  88.   
  89.   return Json(jsonObj, JsonRequestBehavior.AllowGet);   
  90.   
  91.   }   
  92.   
  93.   public ActionResult RedirectResult()   
  94.   
  95.   {   
  96.   
  97.   return Redirect("~/demo.jpg");   
  98.   
  99.   }   
  100.   
  101.   public ActionResult RedirectToRouteResult()   
  102.   
  103.   {   
  104.   
  105.   return RedirectToRoute(new {   
  106.   
  107.   controller = "Hello"action = ""  
  108.   
  109.   });   
  110.   
  111.   }   
  112.   
  113.   public ActionResult ViewResult()   
  114.   
  115.   {   
  116.   
  117.   return View();   
  118.   
  119.   }   
  120.   
  121.   public ActionResult PartialViewResult()   
  122.   
  123.   {   
  124.   
  125.   return PartialView();   
  126.   
  127.   }   
  128.   
  129.   //禁止直接访问的ChildAction   
  130.   
  131.   [ChildActionOnly]   
  132.   
  133.   public ActionResult ChildAction()   
  134.   
  135.   {   
  136.   
  137.   return PartialView();   
  138.   
  139.   }   
  140.   
  141.   //正确使用ChildAction   
  142.   
  143.   public ActionResult UsingChildAction()   
  144.   
  145.   {   
  146.   
  147.   return View();   
  148.   
  149.   }   
  150.   
  151.   }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值