1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Mvc.Ajax;
  7. using System.IO;
  8.  
  9. namespace MVC.Controllers
  10. {
  11.     /// <summary>
  12.     /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
  13.     /// </summary>
  14.     public class ControllerDemoController : Controller
  15.      {
  16.          // [NonAction] - 当前方法仅为普通方法,不解析为 Action
  17.          // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法
  18.  
  19.          /// <summary>
  20.          /// Action 可以没有返回值
  21.          /// </summary>
  22.          public void Void()
  23.          {
  24.              Response.Write(string.Format("<span style='color: red'>{0}</span>", "void"));
  25.          }
  26.  
  27.          /// <summary>
  28.          /// 如果 Action 要有返回值的话,其类型必须是 ActionResult
  29.          /// EmptyResult - 空结果
  30.          /// </summary>
  31.          public ActionResult EmptyResult()
  32.          {
  33.              Response.Write(string.Format("<span style='color: red'>{0}</span>", "EmptyResult"));
  34.              return new EmptyResult();
  35.          }
  36.  
  37.          /// <summary>
  38.          /// Controller.Redirect() - 转向一个指定的 url 地址
  39.          /// 返回类型为 RedirectResult
  40.          /// </summary>
  41.          public ActionResult RedirectResult()
  42.          {
  43.              return base.Redirect("~/ControllerDemo/ContentResult");
  44.          }
  45.  
  46.          /// <summary>
  47.          /// Controller.RedirectToAction() - 转向到指定的 Action
  48.          /// 返回类型为 RedirectToRouteResult
  49.          /// </summary>
  50.          public ActionResult RedirectToRouteResult()
  51.          {
  52.              return base.RedirectToAction("ContentResult");
  53.         }
  54.  
  55.          /// <summary>
  56.          /// Controller.Json() - 将指定的对象以 JSON 格式输出出来
  57.          /// 返回类型为 JsonResult
  58.          /// </summary>
  59.          public ActionResult JsonResult(string name)
  60.          {
  61.              System.Threading.Thread.Sleep(1000);
  62.  
  63.              var jsonObj = new { Name = name, Age = new Random().Next(20, 31) };
  64.              return base.Json(jsonObj);
  65.          }
  66.  
  67.          /// <summary>
  68.          /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本
  69.          /// 返回类型为 JavaScriptResult
  70.          /// </summary>
  71.          public ActionResult JavaScriptResult()
  72.          {
  73.              return base.JavaScript("alert('JavaScriptResult')");
  74.          }
  75.  
  76.          /// <summary>
  77.          /// Controller.Content() - 输出一段指定的内容
  78.          /// 返回类型为 ContentResult
  79.          /// </summary>
  80.          public ActionResult ContentResult()
  81.          {
  82.              string contentString = string.Format("<span style='color: red'>{0}</span>", "ContentResult");
  83.              return base.Content(contentString);
  84.          }
  85.  
  86.          /// <summary>
  87.          /// Controller.File() - 输出一个文件(字节数组)
  88.          /// 返回类型为 FileContentResult
  89.          /// </summary>
  90.          public ActionResult FileContentResult()
  91.          {
  92.              FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
  93.              int length = (int)fs.Length;
  94.              byte[] buffer = new byte[length];
  95.              fs.Read(buffer, 0, length);
  96.              fs.Close();
  97.  
  98.              return base.File(buffer, "image/gif");
  99.          }
  100.  
  101.          // <summary>
  102.          /// Controller.File() - 输出一个文件(文件地址)
  103.          /// 返回类型为 FileContentResult
  104.          /// </summary>
  105.          public ActionResult FilePathResult()
  106.          {
  107.              var path = Request.PhysicalApplicationPath + "Content/loading.gif";
  108.              return base.File(path, "image/gif");
  109.          }
  110.  
  111.          // <summary>
  112.          /// Controller.File() - 输出一个文件(文件流)
  113.          /// 返回类型为 FileContentResult
  114.          /// </summary>
  115.          public ActionResult FileStreamResult()
  116.          {
  117.              FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
  118.  
  119.              return base.File(fs, @"image/gif");
  120.          }
  121.  
  122.          /// <summary>
  123.          /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
  124.          /// </summary>
  125.          public ActionResult HttpUnauthorizedResult()
  126.          {
  127.              return new HttpUnauthorizedResult();
  128.          }
  129.  
  130.          /// <summary>
  131.          /// Controller.PartialView() - 寻找 View ,即 .ascx 文件
  132.          /// 返回类型为 PartialViewResult
  133.          /// </summary>
  134.          public ActionResult PartialViewResult()
  135.          {
  136.              return base.PartialView();
  137.          }
  138.  
  139.          /// <summary>
  140.          /// Controller.View() - 寻找 View ,即 .aspx 文件
  141.          /// 返回类型为 ViewResult
  142.          /// </summary>
  143.          public ActionResult ViewResult()
  144.          {
  145.              // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
  146.              return base.View();
  147.          }
  148.  
  149.          /// <summary>
  150.          /// 用于演示处理 JSON 的
  151.          /// </summary>
  152.          public ActionResult JsonDemo()
  153.          {
  154.              return View();
  155.          }
  156.  
  157.          /// <summary>
  158.          /// 用于演示上传文件的
  159.          /// </summary>
  160.          public ActionResult UploadDemo()
  161.          {
  162.              return View();
  163.          }
  164.  
  165.          /// <summary>
  166.          /// 用于演示 Get 方式调用 Action
  167.          /// id 是根据路由过来的;param1和param2是根据参数过来的
  168.          /// </summary>
  169.          [AcceptVerbs(HttpVerbs.Get)]
  170.          public ActionResult GetDemo(int id, string param1, string param2)
  171.          {
  172.              ViewData["ID"] = id;
  173.              ViewData["Param1"] = param1;
  174.              ViewData["Param2"] = param2;
  175.  
  176.             return View();
  177.          }
  178.  
  179.          /// <summary>
  180.          /// 用于演示 Post 方式调用 Action
  181.          /// </summary>
  182.          /// <remarks>
  183.          /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开
  184.          /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开
  185.          /// [Bind] 声明同样可以作用于 class 上
  186.          /// </remarks>
  187.          [AcceptVerbs(HttpVerbs.Post)]
  188.          public ActionResult PostDemo(FormCollection fc)
  189.          {
  190.              ViewData["Param1"] = fc["param1"];
  191.              ViewData["Param2"] = fc["param2"];
  192.  
  193.              // 也可以用 Request.Form 方式获取 post 过来的参数
  194.  
  195.              // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数 
  196.              // public ActionResult PostDemo(string param1, string param2)
  197.  
  198.              return View("GetDemo");
  199.          }
  200.  
  201.          /// <summary>
  202.          /// 处理上传文件的 Action
  203.          /// </summary>
  204.          /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param>
  205.          [AcceptVerbs(HttpVerbs.Post)]
  206.          public ActionResult UploadFile(HttpPostedFileBase file1)
  207.          {
  208.              // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
  209.              // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase;
  210.  
  211.              string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload", Path.GetFileName(file1.FileName));
  212.              file1.SaveAs(targetPath);
  213.  
  214.              return View("UploadDemo");
  215.          }
  216.      }
  217. }
 

Asp.Net Mvc 返回类型总结的更多相关文章

  1. ASP.NET MVC 描述类型(二)

    ASP.NET MVC 描述类型(二) 前言 上个篇幅中说到ControllerDescriptor类型的由来过程,对于ControllerDescriptor类型来言ActionDescriptor ...

  2. ASP.NET MVC 描述类型(一)

    ASP.NET MVC 描述类型(一) 前言 在前面的好多篇幅中都有提到过ControllerDescriptor类型,并且在ASP.NET MVC 过滤器(一)篇幅中简单的描述过,今天我们就来讲一下 ...

  3. 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    问题背景: 在使用asp.net mvc 结合jquery esayui做一个系统,但是在使用使用this.json方法直接返回一个json对象,在列表中显示时发现datetime类型的数据在转为字符 ...

  4. 解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题

    DateTime类型数据格式问题 问题 在使用ASP.NET MVC 在写项目的时候发现,返回给前端的JSON数据,日期类型是 Date(121454578784541) 的格式,需要前端来转换一下才 ...

  5. Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据

    我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...

  6. ASP.NET MVC 枚举类型转LIST CONTROL控件

    在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...

  7. ASP.NET MVC 中使用JavaScriptResult asp.net mvc 返回 JavaScript asp.mvc 后台返回js

    return this.Content("<script>alert('暂无!');window.location.href='/Wap/Index';</script&g ...

  8. ASP .NET Controller返回类型

    返回类型 return View(model); 即返回htmlreturn Json("String"); 返回Json格式的数据return File(new byte[] { ...

  9. 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    当用ajax异步时,返回JsonResult格式的时候,发现当字段是dateTime类型时,返回的json格式既然是“/Date(1435542121135)/” 这样子的,当然这不是我们想要的格式. ...

随机推荐

  1. 鸟哥Linux私房菜基础学习篇学习笔记1

    鸟哥Linux私房菜基础学习篇学习笔记1 第三章 主导分区(MBR),当系统在开机的时候会主动去读取这个区块的内容,必须对硬盘进行分区,这样硬盘才能被有效地使用. 所谓的分区只是针对64Bytes的分 ...

  2. mysql:赋予用户权限、查看及修改端口号

    一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...

  3. 前端 -----函数和伪数组(arguments)

    函数   函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. 简化编程,让编程模块化. ...

  4. 数据库-mysql-DDL-表记录操作

  5. linux软件安装、rpm操作命令、本地yum配置(有什么用)

    1.yum是什么? yum的全称是yellow dog updater,modified,是一个shell前端软件包管理器;基于RPM包管理,能够从指定的服务器下载RPM包并自动安装,可以自动处理依赖 ...

  6. WinSCP安装与使用

      WinSCP 是一个 Windows 环境下使用的 SSH(Source Shell)的开源图形化 SFTP(SSH File Transfer Protocol) 客户端.同时支持 SCP(So ...

  7. SQL Server 数据恢复到指点时间点(完整恢复)

    SQL Server 数据恢复到指点时间点(完整恢复) 高文龙关注2人评论944人阅读2017-03-20 12:57:12 SQL Server 数据恢复到指点时间点(完整恢复) 说到数据库恢复,其 ...

  8. Confluence 6 自定义管理员联系信息

    你可以自定义在 联系站点管理员(Contact Site Administrators)页面中显示的消息. 希望编辑这个管理员联系消息: 在屏幕的右上角单击 控制台按钮 ,然后选择 General C ...

  9. Confluence 6 修改你站点的外观和感觉

    你可以为你的 Confluence 整个站点修改表现以及外观和感觉,也可以为单独的空间进行修改. 对整个站点进行的修改将会对使用全局外观和感觉(look and feel)的空间一并进行修改.如果某个 ...

  10. J2SE基础小结

    1. 九种基本数据类型的大小,以及他们的封装类. 类型 基本类型 大小(字节) 默认值 封装类 整数型 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 ...