• ActionResult解析


    原文地址:http://blog.csdn.net/gulijiang2008/article/details/7642213

    ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 版本中提供的ActionResult派生类:

    类名 抽象类 父类 功能
    ContentResult     根据内容的类型和编码,数据内容.
    EmptyResult     空方法.
    FileResult abstract   写入文件内容,具体的写入方式在派生类中.
    FileContentResult   FileResult 通过 文件byte[] 写入文件.
    FilePathResult   FileResult 通过 文件路径 写入文件.
    FileStreamResult   FileResult 通过 文件Stream 写入文件.
    HttpUnauthorizedResult     抛出401错误
    JavaScriptResult     返回javascript文件
    JsonResult     返回Json格式的数据
    RedirectResult     使用Response.Redirect重定向页面
    RedirectToRouteResult     根据Route规则重定向页面
    ViewResultBase abstract   调用IView.Render()
    PartialViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 
    重写了父类的FindView方法. 
    寻找用户控件.ascx文件
    ViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 
    重写了父类的FindView方法. 
    寻找页面.aspx文件

    五.实例应用

    1.添加Controller

    安装了ASP.NET MVC后, 在项目上点击右键会找到添加Controller项:

    image

    2.添加Action

    下面这个类提供了返回各种类型的ActionResult的Action实例:

    public class DemoController : Controller
        {
    
            /// <summary>
            /// http://localhost:1847/Demo/ContentResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult ContentResultDemo()
            {
                string contentString = "ContextResultDemo!";
                return Content(contentString);
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/EmptyResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult EmptyResultDemo()
            {
                return  new EmptyResult();
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/FileContentResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FileContentResultDemo()
            {
                FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
                fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
                return File(buffer, @"image/gif");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/FilePathResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FilePathResultDemo()
            {
                //可以将一个jpg格式的图像输出为gif格式
                return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/FileStreamResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FileStreamResultDemo()
            {            
                FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
                return File(fs, @"image/gif");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult HttpUnauthorizedResultDemo()
            {
                return new HttpUnauthorizedResult();
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/JavaScriptResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult JavaScriptResultDemo()
            {
                return JavaScript(@"alert(""Test JavaScriptResultDemo!"")");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/JsonResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult JsonResultDemo()
            {
                var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" };
                return Json(tempObj);
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/RedirectResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult RedirectResultDemo()
            {
                return Redirect(@"http://localhost:1847/Demo/ContentResultDemo");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/RedirectToRouteResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult RedirectToRouteResultDemo()
            {
                return RedirectToAction(@"FileStreamResultDemo");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/PartialViewResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult PartialViewResultDemo()
            {
                return PartialView();
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/RedirectToRouteResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult ViewResultDemo()
            {
                //如果没有传入View名称, 默认寻找与Action名称相同的View页面.
                return View();
            }
    
        }
  • 相关阅读:
    IOS 使用动态库(dylib)和动态加载framework
    iOS 开发者应该知道的 ARM 结构
    解惑好文:移动端H5页面高清多屏适配方案
    js 单例模式的实现方式----闭包和构造函数内部判断
    解决express video 手机无法播放的问题
    前后端通吃的单元测试---mocha
    swift-ios开发pod的使用(1)
    UI 自动化测试工具BackstopJS简介(1)
    阿里妈妈-RAP项目的实践(3)
    阿里妈妈-RAP项目的实践(2)
  • 原文地址:https://www.cnblogs.com/niaowo/p/3778106.html
Copyright © 2020-2023  润新知