博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC学习三:MVC Action Result 返回类型实例
阅读量:2386 次
发布时间:2019-05-10

本文共 2016 字,大约阅读时间需要 6 分钟。

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.   }  

转载地址:http://uljab.baihongyu.com/

你可能感兴趣的文章
物化视图comlete刷新会产生大量的日志
查看>>
Mysql cluster slave server的自动检测与修复
查看>>
solaris同步时钟
查看>>
mysql升级
查看>>
V$sql_text v$sqlarea v$sql 的区别
查看>>
Redis 集群功能说明
查看>>
oracle11gR2在RedHat5上前期安装配置脚本
查看>>
sar的用法
查看>>
Cocos2dx3.2从零开始【四】继续。
查看>>
Unable to execute dex: Multiple dex files define 解决方法
查看>>
Cocos2dx3.2从零开始【五】
查看>>
字符画
查看>>
JS读取DropDownList中的值
查看>>
进度条例子
查看>>
WordPress注册支持中文用户名的解决办法
查看>>
设置WordPress评论头像为圆角鼠标触碰后旋转效果
查看>>
WordPress:删除多说插件的版权信息
查看>>
查询表中两个条件下的数目,按三列组成表
查看>>
WinForm下禁止TextBox右键菜单
查看>>
C#_winform_DataGridView_的18种常见属性
查看>>