原文:关于Action返回结果类型的事儿(下)

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");
         }
    }
 }

关于Action返回结果类型的事儿(下)的更多相关文章

  1. ASP.NET MVC – 关于Action返回结果类型的事儿(上)

    原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的 ...

  2. 【MVC】关于Action返回结果类型的事儿(上)

    一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回结果类型: 1. ...

  3. 【MVC】关于Action返回结果类型的事儿(下)

    代码

  4. ASP.NET MVC Action返回结果类型【转】

    ASP.NET MVC 目前一共提供了以下几种Action返回结果类型: 1.ActionResult(base) 2.ContentResult 3.EmptyResult 4.HttpUnauth ...

  5. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  6. MVC Action 返回类型[转]

    一.         ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回 ...

  7. MVC方法的返回值类型

    MVC方法返回值类型 ModelAndView返回值类型: 1.当返回为null时,页面不跳转. 2.当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转. 3.当返回值指定了视图名,程序会按照 ...

  8. Action的返回值类型总结

    Action的返回值 MVC 中的 ActionResult是其他所有Action返回类型的基类,下面是我总结的返回类型,以相应的帮助方法: 下面是这些方法使用的更详细的例子 一.返回View     ...

  9. asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比

    通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型.那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致 ...

随机推荐

  1. Java中List转换为数组,数组转List

    今天写代码遇到一个奇怪的问题,具体代码不贴出了,写一个简化的版本.如下: ArrayList<String> list=new ArrayList<String>();     ...

  2. PHPExcell单元格中某些时间格式的内容不能正确获得的处理办法

    今天在写导入功能的时候某个时间格式的单元格内容不能正确获得,得出的是一串非时间戳的数字. 此时可以使用PHPExcell中自带的方法进行处理:PHPExcel_Shared_Date::ExcelTo ...

  3. ajax_post方式

    test_ajax_post.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &quo ...

  4. 安装android studio 出现的路径问题 tools.jar' seems to be not in Android Studio classpath

    尝试一下android studio  ,谁知出现路径问题 'tools.jar' seems to be not in Android Studio classpath. Please ensure ...

  5. java09数组的使用

    /** * 数组:存储相同数据类型的一组数据! * 声明一个数组就是在内存中开辟了一连串的连续空间! * * 数组和String 都是 引用数据类型 * 数组的使用 */ @Test public v ...

  6. C#总结项目《影院售票系统》编写总结完结篇

    回顾:昨天总结了影院售票系统核心部分-售票,整个项目也就完成了2/3了,需求中也要求了对销售信息的保存,今天就继续总结销售信息的保存以及加载销售信息. 分析:退出程序时将已售出票集合中的对象循环写入到 ...

  7. 4、第4次课 CSS代码第三节课20150923

    1.列表li样式及取值意义 list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lower ...

  8. css position 应用(absolute和relative用法)

    1.absolute(绝对定位) absolute是生成觉对定位的元素,脱离了文本流(即在文档中已经不占据位置),参照浏览器的左上角通过top,right,bottom,left(简称TRBL) 定位 ...

  9. MySQL 列子查询及 IN、ANY、SOME 和 ALL 操作符的使用

    列子查询是指子查询返回的结果集是 N 行一列,该结果通常来自对表的某个字段查询返回. 一个列子查询的例子如下: SELECT * FROM article WHERE uid IN(SELECT ui ...

  10. AIDL Service

    开发AIDL服务的步骤 AIDL(Android Interface Definition Language)是Service的一种重要应用,允许一个应用程序访问另一个应用程序中的对象. 建立AIDL ...