zoukankan      html  css  js  c++  java
  • ActionResult 返回类型

    MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中。这些包含在控制器中的方法,我们称为控制器中的 Action,比如:HomeController 中的 Index 方法就是一个 Action,这些 Action 的作用就是处理请求,然后返回对请求的处理结果。

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

    ActionResult 类图:

    ActionResult为根节点。

    MVC中ActionResult是Action的返回结果。ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等。ActionResult是一个抽象类,它定义了唯一的ExecuteResult方法,参数为一个ControllerContext,下面介绍MVC中的ActionResult 的用法!

    ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成相应的结果

    常见的几种ActionResult:

    1、ContentResult

    返回简单的纯文本内容,可通过ContentType属性指定应答文档类型,通过ContentEncoding属性指定应答文档的字符编码。可通过Controller类中的Content方法便捷地返回ContentResult对象。如果控制器方法返回非ActionResult对象,MVC将简单地以返回对象的ToString()内容为基础产生一个ContentResult对象。

    public ContentResult RSSFeed() 
    { 
        Story[] stories = GetAllStories(); // Fetch them from the database or wherever 
        // Build the RSS feed document 
        string encoding = Response.ContentEncoding.WebName; 
        XDocument rss = new XDocument(new XDeclaration("1.0", encoding, "yes"), 
            new XElement("rss", new XAttribute("version", "2.0"), 
                new XElement("channel", new XElement("title", "Example RSS 2.0 feed"), 
                    from story in stories 
                    select new XElement("item", 
                          new XElement("title", story.Title), 
                          new XElement("description", story.Description), 
                          new XElement("link", story.Url) 
                       ) 
                ) 
            ) 
        ); 
         return Content(rss.ToString(), "application/rss+xml"); 
    }

    2、EmptyResult

    返回一个空的结果。如果控制器方法返回一个null,MVC将其转换成EmptyResult对象。

    3、RedirectResult

    表示一个连接跳转,相当于ASP.NET中的Response.Redirect方法。对应的Controller方法为Redirect。

    public override void ExecuteResult(ControllerContext context) {
        if (context == null) {
            throw new ArgumentNullException("context");
        }
        if (context.IsChildAction) {
            throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
        }
        string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
        context.Controller.TempData.Keep();
        context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */);
    }

    4、RedirectToRouteResult

    同样表示一个调转,MVC会根据我们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,然后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute。

    5、ViewResult:

    表示一个视图结果,它根据视图模板产生应答内容。对应Controller方法为View。

    6、PartialViewResult:

    表示一个部分视图结果,与ViewResult本质上一致,只是部分视图不支持母版,对应于ASP.NET,ViewResult相当于一个Page,而PartialViewResult则相当于一个UserControl。它对应的Controller方法为PartialView。


    7、HttpUnauthorizedResult:

    表示一个未经授权访问的错误。MVC会向客户端发送一个401的应答状态。如果在web.config中开启了表单验证(authentication mode="Forms"),则401状态会将Url转向指定的loginUrl链接。


    8、JavaScriptResult:

    本质上是一个文本内容,只是将Response.ContentType设置为 application/x-javascript,此结果应该和MicrosoftMvcAjax.js脚本配合使用,客户端接收到Ajax应答后,将判断Response.ContentType的值,如果是application/x-javascript,则直接eval执行返回的应答内容。此结果类型对应的Controller方法为JavaScript。


    9、JsonResult:

    表示一个JSON结果。MVC将Response.ContentType设置为application/json,并通过JavaScriptSerializer类将指定对象序列化为Json表示方式。需要注意,默认情况下,MVC不允许GET请求返回JSON结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet。此结果对应的Controller方法为Json。

    class CityData { public string city; public int temperature; } 
    public JsonResult WeatherData() 
    { 
        var citiesArray = new[] { 
            new CityData { city = "London", temperature = 68 }, 
            new CityData { city = "Hong Kong", temperature = 84 } 
        }; 
        return Json(citiesArray); 
    }

    10、FilePathResult、FileContentResult、FileStreamResult: 这三个类继承于FileResult,表示一个文件内容,三者的区别在于,FilePath通过路径传送文件到客户端,FileContent通过二进制数据的方式,而FileStream是通过Stream的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。

    FilePathResult:直接将一个文件发送给客户端:

    public FilePathResult DownloadReport() 
    { 
        string filename = @"c:\files\somefile.pdf"; 
        return File(filename, "application/pdf", "AnnualReport.pdf"); 
    }

    FileContentResult:返回byte字节给客户端比如图片:

    public FileContentResult GetImage(int productId) 
    { 
        var product = productsRepository.Products.First(x => x.ProductID == productId); 
        return File(product.ImageData, product.ImageMimeType); 
    }
    <img src="<%: Url.Action("GetImage", "Products",  new { Model.ProductID }) %>" />

    FileStreamResult:返回流:

    public FileStreamResult ProxyExampleDotCom() 
    { 
        WebClient wc = new WebClient(); 
        Stream stream = wc.OpenRead(http://www.studyofnet.com/); 
        return File(stream, "text/html"); 
    }

    总结:

     Result的封装:

    除了通过new对象返回结果外,还可以使用封装后的方法;

    示例:

            public IActionResult Result1()//实例化对象
            {
                JsonResult result = new JsonResult(new { name = "kxy1" });
                return result;
            }
            public IActionResult Result2()//封装方法
            {
                return Json(new { name = "kxy2" });
            }

    扩展ActionResult

    下例将实现一个XmlResult类型,用于返回XML应答内容:

        public class XmlResult : ActionResult
         {
            public XmlResult(Object data)
            {
                this.Data = data;
            }
     
            public Object Data
            {
                get;
                set;
            }
     
            public override void ExecuteResult(ControllerContext context)
            {
                if (Data == null)
                {
                    new EmptyResult().ExecuteResult(context);
                    return;
                }
     
                context.HttpContext.Response.ContentType = "application/xml";
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlSerializer xs = new XmlSerializer(Data.GetType());
                    xs.Serialize(ms, Data);
                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        context.HttpContext.Response.Output.Write(sr.ReadToEnd());
                    }
                }
            }
        } 

     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文件
    public class ActionResultController : Controller  
      {  
      public ActionResult Index()  
      {  
      return View();  
      }  
      public ActionResult ContentResult()  
      {  
      return Content("Hi, 我是ContentResult结果");  
      }  
      public ActionResult EmptyResult()  
      {  
      //空结果当然是空白了!  
      //至于你信不信, 我反正信了  
      return new EmptyResult();  
      }  
    
      public ActionResult FileResult()  
      {  
      var imgPath = Server.MapPath("~/demo.jpg");  
      return File(imgPath, "application/x-jpg", "demo.jpg");  
      }  
      
      public ActionResult HttpNotFoundResult()  
      {  
      return HttpNotFound("Page Not Found");  
      }  
      
      public ActionResult HttpUnauthorizedResult()  
       {  
        //未验证时,跳转到Logon  
        return new HttpUnauthorizedResult();  
       }  
      
      public ActionResult JavaScriptResult()  
       {  
        string js = "alert("Hi, I'm JavaScript.");";  
        return JavaScript(js);  
       }  
      
      public ActionResult JsonResult()  
       {  
        var jsonObj = new  
        {  
        Id = 1,  
        Name = "小铭",  
        Sex = "",  
        Like = "足球"  
        };  
        return Json(jsonObj, JsonRequestBehavior.AllowGet);  
      }  
      
      public ActionResult RedirectResult()  
      
      {  
      
      return Redirect("~/demo.jpg");  
      
      }  
      
      public ActionResult RedirectToRouteResult()  
      
      {  
      
      return RedirectToRoute(new {  
      
      controller = "Hello", action = ""  
      
      });  
      
      }  
      
      public ActionResult ViewResult()  
      
      {  
      
      return View();  
      
      }  
      
      public ActionResult PartialViewResult()  
      
      {  
      
      return PartialView();  
      
      }  
      
      //禁止直接访问的ChildAction  
      
      [ChildActionOnly]  
      
      public ActionResult ChildAction()  
      
      {  
      
      return PartialView();  
      
      }  
      
      //正确使用ChildAction  
      
      public ActionResult UsingChildAction()  
      
      {  
      
      return View();  
      
      }  
      
      }  
  • 相关阅读:
    oracle数据库登录连接很慢;kettle连接oracle 报 IO 错误,socket time out 问题解决记录
    装机攻略
    项目开发规范
    选项卡事件测试
    日常报错记录
    邮箱常用端口及协议
    JS制作二级联动
    使用JQuery对页面进行绑值
    IDEA 报错记录
    论文格式
  • 原文地址:https://www.cnblogs.com/wfy680/p/15150716.html
Copyright © 2011-2022 走看看