ASP.NET MVC 3.0 Controller基础
ASP.NET MVC 3.0 Controller基础
1、Controller类与方法
Controller(控制器)是ASP.NET MVC的核心,负责处理浏览器请求,并作出响应。Cotroller本身是一个类(Class),该类有多个方法(Method)。在这些方法中,只要是公开方法,该方法将被视为一个动作(Action);只要有动作存在,就可以通过该动作方法接收网页请求并决定应响应的视图。
Controller的基本要求:
- Controller必须是公共(Public)类;
- Controller的名称必须以“Controller”结尾;
- 必须继承ASP.NET MVC的Controller类,或继承实现IController接口的自定义类,或自身实现IController接口;
- 所以方法必须为公共(Public)方法。该方法可以没有参数,也可以有多个参数。
2、Action名称选择器
当通过ActionInvoker选取Controller中的公共方法时,默认会用Reflection方式取得Controller中具有相同名称的方法。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Controllers
8 {
9 public class HomeController : Controller
10 {
11 /// <summary>
12 /// http://localhost/Home/Index
13 /// </summary>
14 public ActionResult Index()
15 {
16 return View();
17 }
18 }
19 }

可以通过在Action上使用ActionName属性(Attribute)来指定Action,这就是动作名称选择器(Action Name Selector)。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Controllers
8 {
9 public class HomeController : Controller
10 {
11 /// <summary>
12 /// http://localhost/Home/Default
13 /// </summary>
14 [ActionName("Default")]
15 public ActionResult Index()
16 {
17 return View();
18 }
19 }
20 }

3、动作方法选择器
在通过ActionInvoker选取Controller中的公共方法是,ASP.NET MVC还提供了一个特性,动作方法选择器(Action Method Selector),以帮助ActionInvoker选择适当的Action。
3.1、 NonAction属性
若将NonAction属性应用在Controller中的Action方法上,即使该Action方法是公共方法,也会告知ActionInvoker不要选取这个Action来执行。这个属性主要用来保护Controller中的特定公共方法不会发布到Web上。或是当功能尚未开发完成就要进行部署时,若暂时不想将此方法删除,也可以使用这个属性。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Controllers
8 {
9 public class HomeController : Controller
10 {
11 [NonAction]
12 public ActionResult Index()
13 {
14 return View();
15 }
16 }
17 }

将Action方法的“public”改成“private”,也可以达到同样的目的。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Controllers
8 {
9 public class HomeController : Controller
10 {
11 private ActionResult Index()
12 {
13 return View();
14 }
15 }
16 }

3.2、HttpGet属性、HttpPost属性、HttpDelete属性和HttpInput属性
HttpGet、HttpPost、HttpDelete、HttpInput属性是动作方法选取器的一部分,这些属性常用在需要接收窗口数据的时候。如:创建两个同名的Action,一个应用[HttpGet]属性来显示窗口HTML,另一个应用[HttpPost]属性来接收窗口发送的值。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Controllers
8 {
9 public class HomeController : Controller
10 {
11 [HttpGet]
12 public ActionResult Create()
13 {
14 return View();
15 }
16
17 [HttpPost]
18 public ActionResult Create(Product product)
19 {
20 //UpdateModel(product);
21 return RedirectToAction("Index");
22 }
23 }
24 }

4、ActionResult类
ActionResult类是Action执行的结果,但ActionResult中并不包含执行结果,而是包含执行响应时所需要的信息。当Action返回ActionResult类之后,会由ASP.NET MVC执行。
ASP.NET MVC定义的ActionResult如下表所示:
| 类 | Controller辅助方法 | 用途 |
|---|---|---|
| ContentResult | Content | 返回一段用户自定义的文字内容 |
| EmptyResult | 不返回任何数据,即不响应任何数据 | |
| JsonResult | Json | 将数据序列转化成JSON格式返回 |
| RedirectResult | Redirect | 重定向到指定的URL |
| RedirectToRouteResult | RedirectToAction、RedirectToRoute | 重定向到Action或Route |
| ViewResult | View | 使用IViewInstance接口和IViewEngine接口,实际输出的数据是IViewEngine接口和View |
| PartialViewResult | PartialView | 与ViewResult类相似,返回的是“部分显示” |
| FileResult | File | 以二进制串流的方式返回一个文件数据 |
| JavaScriptResult | JavaScript | 返回JavaScript指令码 |
4.1、ViewResult
ViewResult类是在ASP.NET MVC中最常用的ActionResult类,用于返回一个标准的视图。通过Controller辅助方法,可以定义输出的View名称。
a>、返回默认的页面
返回的默认页面与Action的名称相同

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Web.Controllers
8 {
9 public class HomeController : Controller
10 {
11 public ActionResult Index()
12 {
13 return View();
14 }
15 }
16 }

b>、指定页面名称的响应

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Web.Controllers
8 {
9 public class HomeController : Controller
10 {
11 public ActionResult Index()
12 {
13 return View("Default");
14 }
15 }
16 }

c>、指定的页面不存在

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace Northwind.Web.Controllers
8 {
9 public class HomeController : Controller
10 {
11 public ActionResult Index()
12 {
13 return View("NotExists");
14 }
15 }
16 }

当在Views目录下找不到页面时,出现下面的提示信息。

4.2、PartialViewResult
PartialViewResult与ViewResult非常相似,通过用在前端为Ajax应用程序的情况下,并可以通过Ajax来取得网页中的部分内容。
public ActionResult About()
{
return PartialView();
}
4.3、EmptyResult
有一些Action在执行后其实不需要返回任何数据,例如一个页面执行完后直接转到其他页面的情况。EmptyResult不会执行任何响应客户端的程序,所以也不会返回任何数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Empty()
{
return new EmptyResult();
}
}
}

或

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public void Empty()
{
return;
}
}
}

使用EmptyResult与Response.RedirectPermanent()进行HTTP301跳转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public void Redirect()
{
Response.RedirectPermanent("/Home/Index");
}
}
}

4.4、ContentResult
ContentResult类可以响应文字内容的结果。可以让ContentResult类响应任意指定文字内容、Content-Type和文字编码(Encoding)。
示例响应一段XML文字,并设定响应的Content-Type为text/xml,文本编码格式为Encoding.UTF8.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Content()
{
return Content("<root><text>123</text></root>", "text/xml", System.Text.Encoding.UTF8);
}
}
}

响应HTML字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Content()
{
string strHTML = "<h1>123</h1>";
return Content(strHTML);
}
}
}

ASP.NET MVC会进行判断,只要Action返回的不是ActionResult类,就会将返回的类转换成字符串输出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Northwind.Web.Controllers
{
public class HomeController : Controller
{
public string Content()
{
string strHTML = "<h1>123</h1>";
return strHTML;
}
}
}

6.5、FileResult
ASP.NET MVC 3.0 Controller基础的更多相关文章
- asp.net MVC 4.0 Controller回顾——ModelBinding实现过程
以DefaultModelBinder为例 为简单模型绑定(BindSimpleModel)和复杂模型绑定(BindComplexModel) public virtual object BindMo ...
- 返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller
原文:返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller [索引页][源码下载] 返璞归真 asp.net mvc (7) - asp.net ...
- (转)ASP.NET Mvc 2.0 - 1. Areas的创建与执行
转自:http://www.cnblogs.com/terrysun/archive/2010/04/13/1711218.html ASP.NET Mvc 2.0 - 1. Areas的创建与执行 ...
- 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性
[索引页][源码下载] 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性 作者:webabcd 介绍asp.net mvc 之 asp.net mvc 5.0 新 ...
- 从零开始学习ASP.NET MVC 1.0
转自:http://www.cnblogs.com/zhangziqiu/archive/2009/02/27/ASPNET-MVC-1.html <从零开始学习ASP.NET MVC 1.0& ...
- ASP.NET MVC 4.0的Action Filter
有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你自定义创建action过滤器.Action过滤器是自定义的Attributes,用来 ...
- 返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model
原文:返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model [索引页][源码下载] 返璞归真 asp.net mvc (8) - asp.net mvc ...
- 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性
原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...
- 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作
原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...
随机推荐
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 1.MySQL的安装(linux Ubuntu环境下)
首先先检验一下系统中是否已经安装有mysql: deamon@deamon-H55M-S2:~$ sudo netstat -tap | grep mysql [sudo] password for ...
- C/C++代码检视要点
4.1.1 C/C++代码检视要点 代码检视技能属于开发人员的基本功,能够很大程度地反应出开发人员的能力水平,前面4.4.1节已经讲过提高评审检视的方法.下面以实际的C/C++语言方面的代 ...
- asp.net发送E-mail
发送电子邮件也是项目开发当中经常用到的功能,这里我整理了一个发送电子邮件(带附件,支持多用户发送,主送.抄送)的类库,供大家参考. 先上两个实体类,用于封装成Mail对象. /// <summa ...
- easyui 初体验
简介 jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编写复杂的javas ...
- asp.net+MVC--1
1.MVC入门 1)第一个路由: /*任何应用程序启动时发生的动作都应该存在于单独的类中,并且仅在该方法中按照正确顺序调用*/ protected void Application_St ...
- C#多线程(一)
一.定义与理解 1.定义 线程是操作系统分配CPU时间片的基本单位,每个运行的引用程序为一个进程,这个进程可以包含一个或多个线程. 线程是进程中的执行流程,每个线程可以得到一小段程序的执行时间,在单核 ...
- MySQL数据库远程访问的权限
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%'IDENTIFIED BY 'passwd' WITH GRANT OPTION;
- R语言和大数据
#安装R语言R3.3版本会出现各种so不存在的问题,退回去到R3.1版本时候就顺利安装.在安装R环境之前,先安装好中文(如果没有的话图表中显示汉字成框框了)和tcl/tk包(少了这个没法安装sqldf ...
- 在C#中IEnumerable与IEnumerator
对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...