using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;
 
namespace MVC.Controllers
{
    /// <summary>
    /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
    /// </summary>
    public class ControllerDemoController : Controller
     {
         // [NonAction] - 当前方法仅为普通方法,不解析为 Action
         // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法
 
         /// <summary>
         /// Action 可以没有返回值
         /// </summary>
         public void Void()
         {
             Response.Write(string.Format("<span style='color: red'>{0}</span>", "void"));
         }
 
         /// <summary>
         /// 如果 Action 要有返回值的话,其类型必须是 ActionResult
         /// EmptyResult - 空结果
         /// </summary>
         public ActionResult EmptyResult()
         {
             Response.Write(string.Format("<span style='color: red'>{0}</span>", "EmptyResult"));
             return new EmptyResult();
         }
 
         /// <summary>
         /// Controller.Redirect() - 转向一个指定的 url 地址
         /// 返回类型为 RedirectResult
         /// </summary>
         public ActionResult RedirectResult()
         {
             return base.Redirect("~/ControllerDemo/ContentResult");
         }
 
         /// <summary>
         /// Controller.RedirectToAction() - 转向到指定的 Action
         /// 返回类型为 RedirectToRouteResult
         /// </summary>
         public ActionResult RedirectToRouteResult()
         {
             return base.RedirectToAction("ContentResult");
        }
 
         /// <summary>
         /// Controller.Json() - 将指定的对象以 JSON 格式输出出来
         /// 返回类型为 JsonResult
         /// </summary>
         public ActionResult JsonResult(string name)
         {
             System.Threading.Thread.Sleep(1000);
 
             var jsonObj = new { Name = name, Age = new Random().Next(20, 31) };
             return base.Json(jsonObj);
         }
 
         /// <summary>
         /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本
         /// 返回类型为 JavaScriptResult
         /// </summary>
         public ActionResult JavaScriptResult()
         {
             return base.JavaScript("alert('JavaScriptResult')");
         }
 
         /// <summary>
         /// Controller.Content() - 输出一段指定的内容
         /// 返回类型为 ContentResult
         /// </summary>
         public ActionResult ContentResult()
         {
             string contentString = string.Format("<span style='color: red'>{0}</span>", "ContentResult");
             return base.Content(contentString);
         }
 
         /// <summary>
         /// Controller.File() - 输出一个文件(字节数组)
         /// 返回类型为 FileContentResult
         /// </summary>
         public ActionResult FileContentResult()
         {
             FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
             int length = (int)fs.Length;
             byte[] buffer = new byte[length];
             fs.Read(buffer, 0, length);
             fs.Close();
 
             return base.File(buffer, "image/gif");
         }
 
         // <summary>
         /// Controller.File() - 输出一个文件(文件地址)
         /// 返回类型为 FileContentResult
         /// </summary>
         public ActionResult FilePathResult()
         {
             var path = Request.PhysicalApplicationPath + "Content/loading.gif";
             return base.File(path, "image/gif");
         }
 
         // <summary>
         /// Controller.File() - 输出一个文件(文件流)
         /// 返回类型为 FileContentResult
         /// </summary>
         public ActionResult FileStreamResult()
         {
             FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
 
             return base.File(fs, @"image/gif");
         }
 
         /// <summary>
         /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
         /// </summary>
         public ActionResult HttpUnauthorizedResult()
         {
             return new HttpUnauthorizedResult();
         }
 
         /// <summary>
         /// Controller.PartialView() - 寻找 View ,即 .ascx 文件
         /// 返回类型为 PartialViewResult
         /// </summary>
         public ActionResult PartialViewResult()
         {
             return base.PartialView();
         }
 
         /// <summary>
         /// Controller.View() - 寻找 View ,即 .aspx 文件
         /// 返回类型为 ViewResult
         /// </summary>
         public ActionResult ViewResult()
         {
             // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
             return base.View();
         }
 
         /// <summary>
         /// 用于演示处理 JSON 的
         /// </summary>
         public ActionResult JsonDemo()
         {
             return View();
         }
 
         /// <summary>
         /// 用于演示上传文件的
         /// </summary>
         public ActionResult UploadDemo()
         {
             return View();
         }
 
         /// <summary>
         /// 用于演示 Get 方式调用 Action
         /// id 是根据路由过来的;param1和param2是根据参数过来的
         /// </summary>
         [AcceptVerbs(HttpVerbs.Get)]
         public ActionResult GetDemo(int id, string param1, string param2)
         {
             ViewData["ID"] = id;
             ViewData["Param1"] = param1;
             ViewData["Param2"] = param2;
 
            return View();
         }
 
         /// <summary>
         /// 用于演示 Post 方式调用 Action
         /// </summary>
         /// <remarks>
         /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开
         /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开
         /// [Bind] 声明同样可以作用于 class 上
         /// </remarks>
         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult PostDemo(FormCollection fc)
         {
             ViewData["Param1"] = fc["param1"];
             ViewData["Param2"] = fc["param2"];
 
             // 也可以用 Request.Form 方式获取 post 过来的参数
 
             // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数 
             // public ActionResult PostDemo(string param1, string param2)
 
             return View("GetDemo");
         }
 
         /// <summary>
         /// 处理上传文件的 Action
         /// </summary>
         /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param>
         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult UploadFile(HttpPostedFileBase file1)
         {
             // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
             // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase;
 
             string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload", Path.GetFileName(file1.FileName));
             file1.SaveAs(targetPath);
 
             return View("UploadDemo");
         }
     }
}
 

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. 023_nginx跨域问题

    什么是跨域? 使用js获取数据时,涉及到的两个url只要协议.域名.端口有任何一个不同,都被当作是不同的域,相互访问就会有跨域问题.例如客户端的域名是www.redis.com.cn,而请求的域名是w ...

  2. $Django ajax简介 ajax简单数据交互,上传文件(form-data格式数据),Json数据格式交互

    一.ajax  1 什么是ajax:异步的JavaScript和xml,跟后台交互,都用json  2 ajax干啥用的?前后端做数据交互:  3 之前学的跟后台做交互的方式:   -第一种:在浏览器 ...

  3. 外网zabbix-server使用主动模式监控公司内网windows服务器

    外网zabbix-server使用主动模式监控公司内网windows服务器 1.Zabbix Agent active批量调整客户端为主动模式监控将Template OS Windows模板调整为主动 ...

  4. 24)django-信号

    目录 1)django信号简介 2)django内置信号 3)django自定义信号 一:django信号简介 Django中提供了“信号调度”,用于在框架执行操作时解耦. 通俗来讲,就是一些动作发生 ...

  5. Jenkins三.1 配置maven

    maven配置安装下载 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-b ...

  6. socket通讯---TcpClient

    IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipa = ipe.AddressList[0]; System.Ne ...

  7. VBS将本地的Excel数据导入到SQL Server中

    VBS将本地的Excel数据导入到SQL Server中 高文龙关注0人评论1170人阅读2017-05-14 12:54:44 VBS将本地的Excel数据导入到SQL Server中 最近有个测试 ...

  8. 各数据库连接maven配置

    Derbydb driver maven dependency<dependency> <groupId>org.apache.derby</groupId> &l ...

  9. LeetCode(84): 柱状图中最大的矩形

    Hard! 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度 ...

  10. django配置一个网站建设

    第一步: 安装数据库MySQL,也可以使用pycharm自带的数据库sqllite,大项目要使用数据库.安装请参考上篇. 数据库在pycharm中驱动设置,setting文件中修改驱动文件密码等信息. ...