zoukankan      html  css  js  c++  java
  • 三、理解控制器、控制器动作和ActionResults

    在这篇文章中我们将探讨ASP.NET MVC控制器控制器动作动作结果。看完这篇文章后我们就应当理解控制层如何用户与ASP.NET MVC页面的交互。
    一、理解控制层
    MVC控制层主要是响应来自ASP.NET MVC WEB站点的用户请求。每个浏览器的请求被映射到特相应的控制器上。例如:当我们在地址栏中输入下面URL
    http://localhost/Product/Index/3

    ProductController控制器被触发。ProductController控制器会对浏览器产生响应,例如:控制器向浏览器返回一个视图或控制器重定向到其它的控制器上去。
    要想添加一个新的控制器,请在ASP.NET MVC应用程序的Controllers文件夹上右击,在弹出菜单中选择“Add”-“New Item”弹出MVC控制器模板选择对话框,如下图所示。控制器名子必须包含Controller后缀。例如,ProductController是否确的控制器名,而Product则不正确。


    《图1》
    如我们创建了一个名子为ProductController的新控制器,它的初始代码如下:
    Listing 1 – ProductController.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace MvcApp.Controllers
    {     
    public class ProductController : Controller     
    {          
       public ActionResult Index()          
       {               
        // Add action logic here               
        throw new NotImplementedException();          
       }     
    }
    }
    通过上面的代码我们可以看出,控制器就是一个类,是一个派生自System.Web.Mvc.Controller的类。正是因此它派自这个类,所以控制器有几个好用的方法(稍后再加讨论)

    二、理解控制器的动作
    所谓的控制器动作就是控制器类中的方法,它可被客户端浏览器的URL调用。例如:如果我们客户端发出下面的浏览器请求
    http://localhost/Product/Index/3
    ProductController类的Index()方法被调用。

    控制器动作必须是控制器类的public方法,控制器中任何的public方法都被自动当成控制器动作看待。
    还需要注意的是对于控制器动作,无法实现重载,并且控制器动作不能够是静态方法。除此之外,我们可以使用任何public方法来作为控制器动作。

    (原创:灰灰虫的家 http://hi.baidu.com/grayworm)
    三、理解动作结果

    控制器返回Action Result,Action Result就是控制器在响应浏览器请求时,返回给浏览器的结果在这里我称之为动作结果。
    在ASP.NET MVC框架中支持以下六种动作结果
    ViewResult-提供HTML和标记
    EmptyResult-不提供任何结果
    RedirectResult-重定向到一个新的URL上去
    RedirectToRouteResult-重定向一个新的控制器动作上去
    JsonResult-在Ajax程序提供一个JavaScript 对象Notation
    ContentResult-提供一个文本结果

    所有这些动作结果都派生自ActionResult
    大多数情况下,控制器动作返ViewResult。
    如下面的Index()动作
    Listing 2 – BookController.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace MvcApp.Controllers
    {     
    public class BookController : Controller     
    {          
       public ActionResult Index()          
       {               
        return View();          
       }     
    }
    }
    当动作返回ViewResult时,它会把HTML返回到客户端浏览器。上面的代码中,Index()方法返回一个名子为Index.aspx的视图到浏览器中去。

    大家可以注意到了,在上面的代码中并没有返回ViewResult(),代替它的却是调用控制器基类的View()方法。通常我们并不直接返回动作结果,而是调用控制器基类的下列方法来返回结果:
    View - 返回一个ViewResult动作结果
    Redirect – 返回RedirectResult动作结果
    RedirectToAction – 返回RedirectTORouteResult动作结果
    RedirectToRoute – 返回RedirectToRouteResult动作结果
    Json – 返回JsonResult动作结果
    Content – 返回ContentResult动作结果

    由此可知,当我们想向客户端浏览器返回一个视图的话,我们应当调用View()方法。果我们想重定向到另一个控制器动作的时候,我们应当调用RedirectToAction()方法。例如:下面代码中的Details()动作,它根据传入参数的不同,动态决定返回视图还是重定向到Index()动作。
    Listing 3 – CustomerController.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace MvcApp.Controllers
    {     
    public class CustomerController : Controller     
    {          
       public ActionResult Details(int? Id)          
       {               
        if (Id == null)                    
       return RedirectToAction("Index");               
       return View();          
       }          
       public ActionResult Index()          
       {               
       return View();          
       }     
    }
    }

    ContentResult动作结果比较特殊,我们可以使用ContentResult把动作结果作为文本返回到客户端。例如:下面代码的Index()方法返回的就是一段普通的文本信息,而不是HTML文本
    Listing 4 – StatusController.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace MvcApp.Controllers
    {     
    public class StatusController : Controller     
    {          
       public ContentResult Index()          
       {               
        return Content("Hello World!");          
       }     
    }
    }
    当StatusController.Index()动作被调用时,向浏览器返回的不是一个视图,而是一个无格式文本”Hello World”。
    如果控制器动作返回的不是动作结果,像日期型数据或整型数据,那结果会被自动包装成ContentResult动作结果。如下面的代码中,当我们调用Index()动作时,返回一个日期,该日期被自动包装成ContentResult。

    Listing 5 – WorkController.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace MvcApp.Controllers
    {     
    public class WorkController : Controller     
    {          
       public DateTime Index()          
       {               
        return DateTime.Now;          
       }     
    }
    }
    上面的代码中Index()动作返回的是一个日期型的对象,ASP.NET MVC框架把日期型的数据转换为字符串数据并自动包装到ContentResult中。浏览器中接到到的会是一个无格式文本。
    (原创:灰灰虫的家 http://hi.baidu.com/grayworm)

    总结
    这篇文章主要介绍了ASP.NET MVC 的几个概念控制器、控制动作、控制器的动作结果。
    在第一部分中,我们学习了如何向ASP.NET MVC项目中添加一个新控制器。接着我们又知道了如何把控制类的方法做成控制器动作。最后我们讨论了从控制器中返回的不同类型的动作结果。另外我们又讨论了如何从控制器的动作返回ViewResult、RedirectToActionResult和ContentResult。

  • 相关阅读:
    c# 实现鼠标拖拽TreeView节点
    代码生成组合编码
    .net分布式错误,DTC出错问题
    乱七八糟?Ⅱ.哈哈
    用SQL只获取日期的方法
    C#学习之接口
    webservice 上传图片、下载图片
    Python包系列
    多线程多进程模块
    第九章Admin后台系统
  • 原文地址:https://www.cnblogs.com/zxktxj/p/2461448.html
Copyright © 2011-2022 走看看