ASP.NET MVC 4 (二)控制器
MVC中控制器负责处理请求,由它操作数据模型,最后返回视图给用户。
IController接口
所有的控制器类以Controller结尾,必须实现System.Web.Mvc.IController接口,一个最简单的控制器类可以是:
public class BasicController : IController {public void Execute(RequestContext requestContext) {string controller = (string)requestContext.RouteData.Values["controller"];string action = (string)requestContext.RouteData.Values["action"];if (action.ToLower() == "redirect") {requestContext.HttpContext.Response.Redirect("/Derived/Index");} else {requestContext.HttpContext.Response.Write(string.Format("Controller: {0}, Action: {1}", controller, action));}}}
BasicController类实现了IController的唯一方法Execute(),在上面的例子中直接返回数据到请求响应,我们可以根据自己的需求来灵活的处理客户请求。
Controller类
更多的时候我们直接继承MVC已经定义的控制类System.Web.Mvc.Controller:
public class DerivedController : Controller{public ActionResult Index(){ViewBag.Message = "Hello from the DerivedController Index method";return View("MyView");}}
Controller类封装的IController.Execute()方法会调用我们定义的Action方法,比如这里的Index()。Controller同时为我们提供了众多的属性来方便获取相关数据:
- Request:比如Request.QueryString、Request.Url、Request.Form、Request.HttpMethod等。
- Response:可以直接向Response返回数据到用户。
- RouteData:和路径映射相关的数据,比如RouteData.Route、RouteData.Values。
- HttpContext:比如HttpContext.HttpSessionStateBAse、HttpContext.Items。
- User:当前已会话已认证的用户信息。
- TempData:存储当前用户的一些临时信息,可以传递给View。
一些获取Controller数据的例子:
string userName = User.Identity.Name; string serverName = Server.MachineName; string clientIP = Request.UserHostAddress; DateTime dateStamp = HttpContext.Timestamp; AuditRequest(userName, serverName, clientIP, dateStamp, "Renaming product"); // Retrieve posted data from Request.Form string oldProductName = Request.Form["OldName"]; string newProductName = Request.Form["NewName"]; bool result = AttemptProductRename(oldProductName, newProductName); ViewData["RenameResult"] = result; return View("ProductRenamed");
Action方法参数
控制器Action方法可以带一系列的参数,比如:
... public ActionResult ShowWeatherForecast(string city, DateTime forDate) { // ... implement weather forecast here ... return View(); } ...
这些参数是由MVC的Value providers和model binders所提供的,MVC自动从请求数据等中自动按名称为我们解析参数值并做类型转换,比如上面例子中的city和forDate可能来自于:
string city = (string)RouteData.Values["city"]; DateTime forDate = DateTime.Parse(Request.Form["forDate"]);
响应输出
我们可以从Icontroller的Excecute()方法中直接输出响应,也可以在Controller中使用Response直接输出响应:
public void ProduceOutput(){if (Server.MachineName == "TINY"){Response.Redirect("/Basic/Index");}else{Response.Write("Controller: Derived, Action: ProduceOutput");}}
这里的Action方法ProduceOutput没有返回值,更多的时候,我们返回一个ActionResult对象,比如View("MyView")返回一个ViewResult,ViewResult继承自ActionResult。甚至我们可以创建自定义的ActionResult类来使用:
public class CustomRedirectResult : ActionResult { public string Url { get; set; } public override void ExecuteResult(ControllerContext context) { string fullUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext); context.HttpContext.Response.Redirect(fullUrl); } }
在控制器Action方法中返回自定义的ActionResult对象:
public ActionResultProduceOutput() { if (Server.MachineName == "TINY") { return new CustomRedirectResult { Url = "/Basic/Index" }; } else { Response.Write("Controller: Derived, Action: ProduceOutput"); return null; } }
这里创建了一个实现重定向的ActionResult对象,实际上MVC已经为我们提供了RedirectResult来实现这个功能:
... public ActionResult ProduceOutput() { return new RedirectResult("/Basic/Index"); } ...
或者更方便的调用Controller的Redirect()方法:
... public ActionResult ProduceOutput() { return Redirect("/Basic/Index"); } ...
除了RedirectResult,MVC还提供这些ActionResult:
- ViewResult:渲染一个视图,等同于控制器中调用View()
- PartialViewResult:渲染部分视图,等同于控制器中调用PartialView()
- RedirectToRouteResult:根据指定的路由信息发出一个HTTP 301或者302重定向命令,等同于控制器调用RedirectToAction 、RedirectToActionPermanent 、RedirectToRoute 、RedirectToRoutePermanent。
- HttpUnauthorizedResult:设置HTTP状态码401表示未授权。
- HttpNotFoundResult:返回HTTP 404-NOT FOUD错误,等同于控制器调用HttpNotFound()。
- HttpStatusCodeResult:返回自定义的HTTP状态码
- EmptyResult:不返回任何信息。
返回视图
用得最多的就是在控制器Action方法中返回一个视图,由它渲染HTML返回给用户。我们可以不带任何参数调用View(),MVC会查找和Action方法同名(不带Controller)的View,也可以直接在参数中指定视图的名称:
public ViewResult Index() { return View("Homepage"); }
MVC在应用目录下搜索视图,如果启用了区域Area,搜索路径为:
• /Areas/<AreaName>/Views/<ControllerName>/<ViewName>.aspx • /Areas/<AreaName>/Views/<ControllerName>/<ViewName>.ascx • /Areas/<AreaName>/Views/Shared/<ViewName>.aspx • /Areas/<AreaName>/Views/Shared/<ViewName>.ascx • /Areas/<AreaName>/Views/<ControllerName>/<ViewName>.cshtml • /Areas/<AreaName>/Views/<ControllerName>/<ViewName>.vbhtml • /Areas/<AreaName>/Views/Shared/<ViewName>.cshtml • /Areas/<AreaName>/Views/Shared/<ViewName>.vbhtml
如果没有使用Area,搜索路径为:
• /Views/<ControllerName>/<ViewName>.aspx • /Views/<ControllerName>/<ViewName>.ascx • /Views/Shared/<ViewName>.aspx • /Views/Shared/<ViewName>.ascx • /Views/<ControllerName>/<ViewName>.cshtml • /Views/<ControllerName>/<ViewName>.vbhtml • /Views/Shared/<ViewName>.cshtml • /Views/Shared/<ViewName>.vbhtml
MVC如果按照上述顺序搜索视图,如果找到一个文件就停止搜索,如果没有可用的视图文件会返回资源未找到错误。注意MVC会搜索ASPX视图引擎的老式文件.aspx和.ascx。
View方法还有其他一些重载调用方式:
return View("Index", "_AlternateLayoutPage"); //指定要使用的布局模板 return View("~/Views/Other/Index.cshtml"); //直接指定要使用的视图模板,必须以/或者~/开头。
从控制器向视图传递数据
我们可以在调用View()直接指定要传递给视图的数据:
... public ViewResult Index() { DateTime date = DateTime.Now; return View(date); } ...
在视图中使用传入的数据:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> The day is: @(((DateTime)Model).DayOfWeek)
对于强类型的视图,使用起来更方便:
@model DateTime @{ ViewBag.Title = "Index"; } <h2>Index</h2> The day is: @Model.DayOfWeek
也可以使用ViewBag来传递,它是一个动态的键值对集合:
public ViewResult Index() { ViewBag.Message = "Hello"; ViewBag.Date = DateTime.Now; return View(); }
在视图中使用:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> The day is: @ViewBag.Date.DayOfWeek <p /> The message is: @ViewBag.Message
Viewbag比视图模型好在可以传递多个值,但是VS不会给出IntelliSense提示,只会在渲染视图时给出错误信息。
重定向
在上面我们已经看到可以使用Redirect重定向请求:
public RedirectResult Redirect() { return Redirect("/Example/Index"); }
也可以使用RedirectPermanent永久重定向:
return RedirectPermanent("/Example/Index");
和Redirect临时重定向发出302 HTTP代码不同,RedirectPermanent发回301 HTTP代码,前者指示浏览器重新GET新的URL,而后者指示浏览器缓存重定向信息,以后针对老URL的请求自动转向新URL。
除了重定向到URL,我们可以重定向到路径映射方法:
public RedirectToRouteResult Redirect() { return RedirectToRoute(new { controller = "Example", action = "Index", ID = "MyID" }); }
或者一个Action方法:
public RedirectToRouteResult RedirectToRoute() { return RedirectToAction("Index"); }
可以指定是某个控制器的某个Action方法:
public RedirectToRouteResult Redirect() { return RedirectToAction("Index", "Basic"); }
如果在重定向另一个控制器的Action时我们想要传递数据该怎么办呢?我们可以使用TempData:
public RedirectToRouteResult RedirectToRoute() { TempData["Message"] = "Hello"; TempData["Date"] = DateTime.Now; return RedirectToAction("Index"); }
在新的视图中读取TempData:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> The day is: @(((DateTime)TempData["Date"]).DayOfWeek) <p /> The message is: @TempData["Message"]
和ViewBag只用于控制器和同名视图不同,TempData类似于会话Session数据,可以在不同控制器/视图中共享,但是和Session跨请求不同的是一旦读取了TempData的数据TempData就会被删除。TempData.Peek读取数据但是不删除数据:
DateTime time = (DateTime)TempData.Peek("Date");
TempData.Keep可以标记数据不被删除,但是如果再被读取又会再次标记删除,类似于引用计数器:
TempData.Keep("Date");
返回HTTP状态码
我们可以返回一个HTTP状态码比如404错误:
public HttpStatusCodeResult StatusCode() { return new HttpStatusCodeResult(404, "URL cannot be serviced"); }
或者直接调用:
return HttpNotFound();
401未授权错误:
return new HttpUnauthorizedResult();
以上为对《Apress Pro ASP.NET MVC 4》第四版相关内容的总结,不详之处参见原版 http://www.apress.com/9781430242369。
转载于:https://www.cnblogs.com/GoogleGetZ/p/5835754.html
ASP.NET MVC 4 (二)控制器相关推荐
- ASP.NET MVC (一、控制器与视图)
目录 前言: 1.MVC简介 2.项目创建:(这里使用工具为:Visual Studio 2019) 2.1.文件夹与文件夹介绍: 3.控制器 3.1.添加控制器 3.2.添加视图层 3.3.修改默认 ...
- ASP.NET MVC学习之控制器篇
一.前言 许久之后终于可以继续我的ASP.NET MVC连载了,之前我们全面的讲述了路由相关的知识,下面我们将开始控制器和动作的讲解. ASP.NET MVC学习之路由篇幅(1) ASP.NET MV ...
- ASP.NET MVC之从控制器传递数据到视图四种方式(一)
前言 本系列开始着手讲述ASP.NET MVC,也算是自己对基础的回顾以及进一步深入学习,保证每天发表一篇该系列文章,关于基本知识则不再叙述,园子有关文章不胜枚举,这一系列若有叙述不当或错误之处,欢迎 ...
- ASP.NET MVC系列:添加控制器
基于MVC的应用程序包含三个部分 Models(模型):对应用程序的数据进行处理 Views(视图):动态生成HTML,显示数据 Controllers(控制器):应用程序中处理用户交互的部分,处理浏 ...
- 光脚丫学ASP.NET MVC(0005):控制器概述
光脚丫思考 QQ:524130780 邮箱:524130780@QQ.COM 博客:http://blog.csdn.net/GJYSK 视频演示:http://u.115.com/file/aqa2 ...
- ASP.NET MVC入门——添加控制器
MVC 代表 模型-视图-控制器. MVC 是用于开发应用程序的一种模式,该模式设计良好.可测试且易于维护. 基于 MVC 的应用程序包含: M 模式:类,这些类表示应用程序的数据,并使用验证逻辑来强 ...
- ASP.NET MVC视图和控制器之间的传值总结(一)
一.Control往View传递值 1.1 ViewBag ViewBag的后面接的是动态对象,它会在程序运行的时候动态解析.具体用法为ViewBag.Key,Key为Data在View中的唯一识别 ...
- asp.net mvc 生成二维码
生成二维码,帮助类: using Gma.QrCodeNet.Encoding; using Gma.QrCodeNet.Encoding.Windows.Render; using System; ...
- 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC
七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)-- ...
- ASP.NET MVC 随笔汇总
ASP.NET MVC 随笔汇总 为了方便大家浏览所以整理一下,有的系列篇幅中不是很全面以后会慢慢的补全的. 学前篇之: ASP.NET MVC学前篇之扩展方法.链式编程 ASP.NET MVC学前篇 ...
最新文章
- Android UI体验之全屏沉浸式透明状态栏效果
- 极大似然估计的理解与应用
- Android面试题目之二:整形转换为字符串
- 【C++】修改const变量的值
- 7——条件判断、三目运算、条件循环、迭代循环
- Java实现双向链表
- TSS描述符表的作用
- java.io.CharConversionException isHexDigit JS转码问题
- stm32数据手册boot_STM32的ISP下载的原理是什么呢?
- POJ - 1966 Cable TV Network(最小割-最大流)
- 数据库事务的概念及其实现原理
- 人车识别实验丨华为ModelArts VS 百度Easy DL硬核体验
- Python如何连接Mysql及基本操作
- 计算机课程新讨论话题,信息技术组内主题讨论-.DOC
- 数学之美——google大脑和人工神经网络
- 关于在SW中怎么放样凸台基体
- Python3:类和对象-烤地瓜
- 虹科技术 | 快速准确测量0.05m-500m--虹科dimetix激光测距传感器的优势
- eclipse中安装的lombok插件不起作用,报错:找不到getter
- 程序猿推送利器:使用Python+Server酱打造微信通知推送利器
热门文章
- LOJ P10147 石子合并 题解
- [ROS-Beginner]1.安装与配置ROS环境
- [转载]git 设置第一次输入密码之后不用输入密码
- centos 网络开启
- 写一个程序,打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”...
- PHP模拟实现url rewrite
- 使用 mod_rewrite 来修改 Confluence 6 的 URLs
- Direct3D光与材质的颜色值
- Python+Selenium ----unittest单元测试框架
- 如何在VS 2010中使用 VS2013的解决方案