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. struts2框架之OGNL(参考第三天学习笔记)

    ognl 1. 什么是ognl 对象图导航语言 Struts内置的表达式语言,它比EL要强大很多. ------------------ 2. 单独学习ognl * EL它操作的数据来自于:四大域:p ...

  2. Vue 核心之数据劫持

    前端界空前繁荣,各种框架横空出世,包括各类mvvm框架横行霸道,比如Angular.Regular.Vue.React等等,它们最大的优点就是可以实现数据绑定,再也不需要手动进行DOM操作了,它们实现 ...

  3. QT 出现信号槽不触发的问题

    主要有以下三点: 1)槽函数未声明为 slots 类型, 信号函数未声明为 signals所致 2)槽函数和信号函数的参数不一致所致 3)connect关联时失败

  4. mysql 5.6升级到5.7.22

    下载对应的包 wget  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar 备份数据 ...

  5. 粘包-socketserver实现并发

  6. 执行原生SQL语句的方式

    原生sql语句 cursor方法:from api.models import *from django.db import connection,connectionscursor=connecti ...

  7. mixins,generics(ApiView)

    #生成序列化对象class BookModelSerizter(serializers.ModelSerializer): class Meta: model=Book fields='__all__ ...

  8. css之坑

    1.background-size要放在background后边才会生效. 2.隐藏滚动条,内容可以滑动 body::-webkit-scrollbar { display: none /* 隐藏滚动 ...

  9. destoon使用

    使用小计 1.判断是否是手机端 {$DT_TOUCH}模板中使用 2.判断句 {if}  {/if} 3.表单管理 扩展功能-----表单管理:添加表单---->管理表单选项------> ...

  10. linux 使用的部分命令

    搜索所有运行着的线程 ps -A | grep apt-get 你会得到类似下面的输出: root ? Ss : : /bin/sh /usr/lib/apt/apt.systemd.daily _a ...