原文URL:http://www.asp.net/learn/mvc/tutorial-03-cs.aspx

Understanding Controllers

1. Controller定义
  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.

A controller is a class that derives from the base System.Web.Mvc.Controller class.

Understanding Controller Actions

1. Action定义
  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.

2. Action的使用注意事项
  2.1 A controller action must be a public method of a controller class.
  2.2 A method used as a controller action cannot be overloaded.
  2.3 A controller action cannot be a static method.

Understanding Action Results

1. Action Result定义
  A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

2. Action Results类型
  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.

2. Action Results类型使用说明

In most cases, a controller action returns a ViewResult.

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.

The ContentResult action result is special. You can use the ContentResult action result to return an action result as plain text.

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.

转载于:https://www.cnblogs.com/jacktang/archive/2010/03/01/1675881.html

ASP.NET MVC Controller Overview摘录相关推荐

  1. ASP.NET MVC Controller激活系统详解:默认实现

    Controller激活系统最终通过注册的ControllerFactory创建相应的Conroller对象,如果没有对ControllerFactory类型或者类型进行显式注册(通过调用当前Cont ...

  2. .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)

    阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...

  3. mvc移动创建oracle表,使用 ASP.NET MVC (C#)在15分钟内创建电影数据库应用程序 | Microsoft Docs...

    使用 ASP.NET MVC 在 15 分钟内创建电影数据库应用程序 (C#)Create a Movie Database Application in 15 Minutes with ASP.NE ...

  4. How ASP.NET MVC Works?

    一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇] MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在" ...

  5. .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象

    原文地址:http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html ------------------------------- ...

  6. ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[上篇]

    Action方法的执行具有两种基本的形式,即同步执行和异步执行,而在ASP.NETMVC的整个体系中涉及到很多同步/异步的执行方式,虽然在前面相应的文章中已经对此作了相应的介绍,为了让读者对此有一个整 ...

  7. Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC

    What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Valida ...

  8. [ASP.NET MVC 小牛之路]10 - Controller 和 Action (2)

    继上一篇文章之后,本文将介绍 Controller 和 Action 的一些较高级特性,包括 Controller Factory.Action Invoker 和异步 Controller 等内容. ...

  9. 通过源代码研究ASP.NET MVC中的Controller和View(二)

    通过源代码研究ASP.NET MVC中的Controller和View(一) 在开始之前,先来温习下上一篇文章中的结论(推论): IView是所有HTML视图的抽象 ActionResult是Cont ...

最新文章

  1. ROS知识:关于如何命名的约定
  2. nginx 上传文件漏洞_nginx爆惊天漏洞 上传图片就能入侵服务器
  3. android愤怒小鸟游戏、自定义View、掌上餐厅App、OpenGL自定义气泡、抖音电影滤镜效果等源码...
  4. HDU 2845 Beans
  5. CENTOS 7 踢用户_从零学ELK系列(三):Centos安装Docker(超详细图文教程)
  6. firebug console说明
  7. yum源分类:Linux
  8. android自动化必备之界面元素
  9. JDK的环境变量配置(详细步骤)
  10. 十分钟读完《金字塔原理》
  11. 1219v网卡驱动+linux,黑苹果Intel板载网卡驱动-IntelMausiEthernet.kext下载 V2.5.0d0-PC6苹果网...
  12. 什么是OSINT?以及如何使用开源网络情报工具?
  13. 22年全国数学建模比赛ABC题思路模型
  14. 如何提高产品质量-开发维度
  15. 2021年装载机司机(建筑特殊工种)找解析及装载机司机(建筑特殊工种)考试总结
  16. jsp中四个作用域的区别
  17. 百度近年来主要算法2017
  18. m4a html 播放器,HTML5 Audio m4a
  19. LATEST DETECTED DEADLOCK
  20. Java周记(第二周)

热门文章

  1. Django中的路由分发
  2. Hive UDF 中使用hdfs中的文件
  3. @SpringBootApplication
  4. 想学数据分析(人工智能)需要学哪些课程?
  5. [转]撞车之后,不要傻里傻气的!这里有绝招!
  6. 在ASP.NET 3.5中使用新的ListView控件1
  7. docker常见面试题_测试面试题集锦(四)| Linux 与 Python 编程篇(附答案)
  8. Incorporating Lexical Priors into Topic Models(即交互式主题模型的应用)论文阅读
  9. aarch64(ARMv8)交叉编译环境下载
  10. Jetson-TX2安装opencv教程