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

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

  
view plain copy to clipboard print ?
  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  
ContentResult   EmptyResult   FileResult   HttpStatusCodeResult   HttpNotFoundResult   HttpUnauthorizedResult   JavaScriptResult   JsonResult   RedirectResult   RedirectToRouteResult   ViewResultBase   PartialViewResult   ViewResult

  示例代码:   

view plain copy to clipboard print ?
  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.   }  

你可能感兴趣的:(mvc,action)