In this tutorial, Stephen Walther introduces you to ASP.NET MVC controllers. You learn how to create new controllers and return different types of action results.

  This tutorial explores the topic of  ASP.NET MVC controllers, controller actions, and action results. After  you complete this tutorial, you will understand how controllers are  used to control the way a visitor interacts with an ASP.NET MVC website.(用ASP.NET MVC WEBSITE 如何使用控制器去访问者进行互动)

  Understanding Controllers

  MVC controllers are responsible for  responding to requests made against an ASP.NET MVC website. Each browser  request is mapped to a particular controller. For example, imagine that  you enter the following URL into the address bar of your browser:

  http://localhost/Product/Index/3

  In this case, a controller named ProductController  is invoked. The ProductController is responsible for generating the  response to the browser request. For example, the controller might return  a particular view back to the browser or the controller might redirect  the user to another controller.

  Listing 1 contains a simple controller  named ProductController.

  Listing1  - Controllers\ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax; namespace MvcApplication1.Controllers
{
public class ProductController : Controller
{
//
// GET: /Products/ public ActionResult Index()
{
// Add action logic here
return View();
} }
}

  As you can see from Listing 1, a controller  is just a class (a Visual Basic .NET or C# class). A controller is a  class that derives from the base System.Web.Mvc.Controller class. Because  a controller inherits from this base class, a controller inherits several  useful methods for free (We discuss these methods in a moment).

  Understanding Controller  Actions

  A controller exposes controller actions.  An action is a method on a controller that gets called when you enter  a particular URL in your browser address bar. For example, imagine that  you make a request for the following URL:

  http://localhost/Product/Index/3

  In this case, the Index() method is  called on the ProductController class. The Index() method is an example  of a controller action.

  A controller action must be a public  method of a controller class. C# methods, by default, are private methods.  Realize that any public method that you add to a controller class is  exposed as a controller action automatically (You must be careful about  this since a controller action can be invoked by anyone in the universe  simply by typing the right URL into a browser address bar).

  There are some additional requirements  that must be satisfied by a controller action. A method used as a controller  action cannot be overloaded. Furthermore, a controller action cannot  be a static method. Other than that, you can use just about any method  as a controller action.

  Understanding Action  Results

  A controller action returns something  called an action result. An action result is what a controller  action returns in response to a browser request.

  The ASP.NET MVC framework supports  several types of action results including:

  1. ViewResult - Represents   HTML and markup.
  2. EmptyResult - Represents   no result.
  3. RedirectResult - Represents   a redirection to a new URL.
  4. JsonResult - Represents   a JavaScript Object Notation result that can be used in an AJAX application.
  5. JavaScriptResult - Represents   a JavaScript script.
  6. ContentResult - Represents   a text result.
  7. FileContentResult - Represents   a downloadable file (with the binary content).
  8. FilePathResult - Represents   a downloadable file (with a path).
  9. FileStreamResult - Represents   a downloadable file (with a file stream).

  All of these action results inherit  from the base ActionResult class.

  In most cases, a controller action  returns a ViewResult. For example, the Index controller action in Listing  2 returns a ViewResult.

Listing 2  - Controllers\BookController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
public class CustomerController : Controller
{
public ActionResult Details(int? id)
{
if (!id.HasValue)
return RedirectToAction("Index"); return View();
} public ActionResult Index()
{
return View();
} }
}

  When an action returns a ViewResult,  HTML is returned to the browser. The Index() method in Listing 2 returns  a view named Index to the browser.

  Notice that the Index() action in Listing  2 does not return a ViewResult(). Instead, the View() method of the  Controller base class is called. Normally, you do not return an action  result directly. Instead, you call one of the following methods of the  Controller base class:

  1. View - Returns a ViewResult   action result.
  2. Redirect - Returns a RedirectResult   action result.
  3. RedirectToAction - Returns   a RedirectToRouteResult action result.
  4. RedirectToRoute - Returns   a RedirectToRouteResult action result.
  5. Json - Returns a JsonResult   action result.
  6. JavaScriptResult - Returns   a JavaScriptResult.
  7. Content - Returns a ContentResult   action result.
  8. File - Returns a FileContentResult,   FilePathResult, or FileStreamResult depending on the parameters passed   to the method.

  So, if you want to return a View to  the browser, you call the View() method. If you want to redirect the  user from one controller action to another, you call the RedirectToAction()  method. For example, the Details() action in Listing 3 either displays  a view or redirects the user to the Index() action depending on whether  the Id parameter has a value.

Listing 3 - CustomerController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
public class CustomerController : Controller
{
public ActionResult Details(int? id)
{
if (!id.HasValue)
return RedirectToAction("Index"); return View();
} public ActionResult Index()
{
return View();
} }
}

  The ContentResult action result is  special. You can use the ContentResult action result to return an action  result as plain text. For example, the Index() method in Listing 4 returns  a message as plain text and not as HTML.

Listing 4 - Controllers\StatusController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
public class StatusController : Controller
{ public ActionResult Index()
{
return Content("Hello World!");
} }
}

  When the StatusController.Index() action  is invoked, a view is not returned. Instead, the raw text "Hello World!"  is returned to the browser.

  If a controller action returns a result  that is not an action result - for example, a date or an integer - then the result is wrapped in a ContentResult automatically. For example,  when the Index() action of the WorkController in Listing 5 is invoked,  the date is returned as a ContentResult automatically.

Listing 5 - WorkController.cs

using System;
using System.Web.Mvc; namespace MvcApplication1.Controllers
{
public class WorkController : Controller
{ public DateTime Index()
{
return DateTime.Now;
} }
}

  The Index() action in Listing 5 returns  a DateTime object. The ASP.NET MVC framework converts the DateTime object  to a string and wraps the DateTime value in a ContentResult automatically.  The browser receives the date and time as plain text.

  Summary

  The purpose of this tutorial was to  introduce you to the concepts of ASP.NET MVC controllers, controller  actions, and controller action results. In the first section, you learned  how to add new controllers to an ASP.NET MVC project. Next, you learned  how public methods of a controller are exposed to the universe as controller  actions. Finally, we discussed the different types of action results  that can be returned from a controller action. In particular, we discussed  how to return a ViewResult, RedirectToActionResult, and ContentResult  from a controller action.

  译文:http://www.cnblogs.com/JimmyZhang/archive/2009/03/14/1411344.html

ASP.NET MVC- Controllers and Routing- Controller Overview的更多相关文章

  1. Post Complex JavaScript Objects to ASP.NET MVC Controllers

    http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/     Post ...

  2. 9、ASP.NET MVC入门到精通——Controller(控制器)

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 Controller主要负责响应用户的输入.主要关注的是应用程序流,输入数据的处理,以及对相关视图(View)输出数据的提供. 继承自:Sy ...

  3. ASP.NET MVC中将数据从Controller传递到视图

    ASP.NET MVC中将数据从Controller传递到视图方法 1.ViewData ViewData的类型是字典数据,key-value 如:ViewData["Data"] ...

  4. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  5. 白话ASP.NET MVC之二:Controller激活系统的概览

    前文简介:我们抽象类路由规则的对象,RouteBase是路由对象的抽象基类,ASP.NET 的路由系统中有唯一一个从RouteBase继承的路由对象,那就是Route类型了.我们注册了路由对象Rout ...

  6. 【ASP.NET MVC】View与Controller之间传递数据

    1   概述 本篇文章主要从操作上简要分析Controller<=>View之间相互传值,关于页面之间传值,如果感兴趣,可参考我另外一篇文章ASP.NET 页面之间传值的几种方式 . Co ...

  7. ASP.NET MVC 第三回 Controller与View

    这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项   之后出现一 ...

  8. asp.net MVC 5 路由 Routing

    ASP.NET MVC ,一个适用于WEB应用程序的经典模型 model-view-controller 模式.相对于web forms一个单一的整块,asp.net mvc是由连接在一起的各种代码层 ...

  9. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...

  10. ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...

随机推荐

  1. 小笔记(一):ajax传递数组及将ajax返回数据赋值

    当使用ajax传递数据时,有可能传递多个数据,这是使用以下方法传递数据就会显得数据过多且混杂 $.ajax({ type:'post', url:url, data:{data:data,conten ...

  2. 怎样清除td和input之间空隙

    <style> input {background:red;border:none;height:30px;margin:0px} td {background-color:blue;pa ...

  3. cygwin下的第一次编程记录

    今天在window7上安装了cygwin,体验了一把unix,编了个小c,c++程序 先把过程记录下来 1.先删除以前建立的source文件夹 rm -rf source linux删除目录很简单,很 ...

  4. 要将表的限制条件写到与该表同级别的where中

    测试目的:将朱查询的限制条件放到子查询的where中,查看性能影响. 测试数据:create table t1 as select object_id,object_name from dba_obj ...

  5. 1.MVC框架开发(初识MVC)

    1.约定大于配置 Content:存放静态文件(样式表.静态图片等) Controllers:存放控制器类 Models:存放数据模型文件 Scripts:存放脚本文件 Views:存放视图文件,里面 ...

  6. 支持HTML5 SqlLite的AndroidApp

    简介: 想要建立一个支持HTML5的Android App; 这个HTML5的程序需要使用本地存储,特别是sqllite; 用eclipse创建了一个app,这个app默认在res/layout建了两 ...

  7. 反编译Android APK及防止APK程序被反编译

    怎么逆向工程对Android Apk 进行反编译 google Android开发是开源的,开发过程中有些时候会遇到一些功能,自己不知道该怎么做,然而别的软件里面已经有了,这个时候可以采用反编译的方式 ...

  8. [转贴]sizeof 和strlen的区别

    1. 编译时计算运算符sizeof,可用类型或变量做参数,计算占用内存的大小.sizeof后若是类型必须加括弧,若是变量名可不加括弧.sizeof(x)可用来定义数组维数.如:printf(" ...

  9. JAVA 抛出与声明异常

    在编程过程中,我们往往会遇到这种情况,在当前环境中无法解决,比如用户传入的参数错误,IO设备问题等.此时,就要从当前环境中抛出异常提交给上级来处理. 在JAVA语言中,使用throw关键字来抛出异常. ...

  10. 《深入理解linux内核架构》第二章 进程管理和调度

    2.1进程优先级 进程优先级 硬实时进程 软实时进程 抢占式多任务处理 2.2进程生命周期 用户太切换到核心态的办法 系统调用 中断 抢占调度模型优先级普通进程<系统调用<中断 普通进程可 ...