1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(五)Controller

Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(五)Controller

时间:2023-01-08 01:52:38

相关推荐

Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(五)Controller

在 mvc的架构里,controller用来响应用户的输入,同时协调view和model以及数据访问层。contorller也是一个类,包含的方法,这些方法是在routing framework处理一个请求的时候被调用的。

我们可以看一下,如果我们建立程序的时候,选择InerterTemplate为我们自动生成的HomeController的代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace InternetEBuy.Controllers{public class HomeController : Controller{public ActionResult Index(){ViewBag.Message = "Modify this template to jump-start your MVC application.";return View();}public ActionResult About(){ViewBag.Message = "Your app description page.";return View();}public ActionResult Contact(){ViewBag.Message = "Your contact page.";return View();}}}

我们可以看一下对应的index.cshtml

@{ViewBag.Title = "Home Page";}@section featured {<section class="featured"><div class="content-wrapper"><hgroup class="title"><h1>@ViewBag.Title.</h1>@*我们可以自己尝试一下,是忽略大小写的!*@<h2>@ViewBag.message</h2></hgroup><p>To learn more about MVC visit<a href="/mvc" title=" MVC Website">/mvc</a>.The page features <mark>videos, tutorials, and samples</mark> to help you get the most from MVC.If you have any questions about MVC visit<a href="/1146.aspx/1?MVC" title=" MVC Forum">our forums</a>.</p></div></section>}<h3>We suggest the following:</h3><ol class="round"><li class="one"><h5>Getting Started</h5> MVC gives you a powerful, patterns-based way to build dynamic websites thatenables a clean separation of concerns and that gives you full control over markupfor enjoyable, agile development. MVC includes many features that enablefast, TDD-friendly development for creating sophisticated applications that usethe latest web standards.<a href="/fwlink/?LinkId=245151">Learn more…</a></li><li class="two"><h5>Add NuGet packages and jump-start your coding</h5>NuGet makes it easy to install and update free libraries and tools.<a href="/fwlink/?LinkId=245153">Learn more…</a></li><li class="three"><h5>Find Web Hosting</h5>You can easily find a web hosting company that offers the right mix of featuresand price for your applications.<a href="/fwlink/?LinkId=245157">Learn more…</a></li></ol>

这里想说的一点就是,我上面有注释的内容, 忽略大小写的。

controller动作

我们可以看到controller类和我们在看到的.cs类没有什么区别,实际上,承担主要工作的是里面的方法(在 mvc里我们把它叫做动作),它们会在处理请求时被调用。

举个例子,比如,我们输入/Home/Index,routing framework就会知道用homecontroller中的index方法来处理这个请求,然后 mvc framework就会生成一个homecontroller的实例,并执行对应的index方法。接下来,Index方法就会通过ViewBag属性来把传输数据,通过view()方法,告诉 mvc framework 显示View目录下名称为Index的view,然后返回的actionResult类型是viewResult类型的。

ActionResutl

我们应该注意的是,controller是告诉framework接下来做什么,而没有告诉怎么做。关于怎么做,是最好每个action都包含的返回值来完成的。比如说,当一个controlller决定显示一个view的时候,它会通过返回一个viewresult来告诉framework显示view。它本身不会渲染view,这种松耦合也是一个分离思想的体现。

事实上,每一个action都应该返回一个actionresult,但是很少情况你需求手动地创建它们,只需求调用system.mvc.contorller父类提供的辅助方法,如下:

从上面的列表,我们可看到,framework提供了我们需要的大部情况的返回值,当然,如果没有提供,framework也提供给我们也可以自己手工创建的能力。

我们也可看到,返回的类型为actionresult,这里用到了多态。还要说明一点的是,如果返回的类型在actionresult里没有找到的话,那么framework会自动将它封装成一个contentResult,然后渲染生成的内容。

action parameter

事实上,action可以获取请求的信息,并通过 mvc framework填充到指定的参数里,这就是 mvc 里的功能强大的model binding。

在看model binding之前,我们还是先来看一下传统的方式。

public ActionResult Create(){var auction = new Models.Auction(){Title = Request["title"],CurrentPrice = decimal.Parse(Request["currentPrice"]),StartTime = DateTime.Parse(Request["startTime"]),EndTime = DateTime.Parse(Request["endTime"])};//return View();}

在这个create action里,要用一个请求传进来的值来实例化一个auction对象,因为auction的属性类型,有很多都不是字符串类型的,所以需要进行转换。从上面的代码,我们可以看到,这些代码其实是很“脆弱的”,因为,只要其中的一个转换失败,那么这个action就会失败。当然,我们可以用tryParse方法来进行验证,但是,这就会增加很多代码,也不具有可读性。 所以也就产生的model Binding

基本的model Binding

我们还是直接看代码:

public ActionResult Create(string title, decimal currentPrice,DateTime startTime, DateTime endTime){var auction = new Auction() {Title = title,CurrentPrice = currentPrice,StartTime = startTime,EndTime = endTime,};// ...}

当前的action直接把要接收的值写到了参数里,当 mvc framework执行的时候,它会试图从请求里用相同的值填充对应的参数, 这里,我们就想到了,这里参数名称的一致就就尤为重要了。在里还要说明一点就是,framework不仅会查看请求对象里是否有对应的值,而且还会查看route data,query string parameter,post values,甚至是序列化的JSON对象。比如下面的代码是从一个连接字符串中获取:

绑定复杂对象

下面我们将演示直接把我们获取的值绑定到Auction对象里:

public ActionResult Create(Auction auction){// ...}

这样是不是更加简洁了,我们同时也看到了model binding的强大之处。

action filters

action filter帮助我们很好的解决了交差访问(关注)的问题,通过给controller action 添加ActionFilterAttribute,来控制action执行,下面是一个阻止匿名用户访问的aciton例子:

[Authorize]public ActionResult Profile(){// Retrieve profile information for current userreturn View();}

mvc framework提供了相当多的filter来解决我们常见的一些问题,它的好处:简洁、松耦合。

我们可以创建Action filters,通过继承ActionFilterAttribute或者其它的 mvc framework的action filter.(action filter可以很方便的给我们的网站添加用户自定义的逻辑)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。