MVC学习笔记

本笔记记录每次看的需要记忆的东西,主要为下次回忆记录下,所谓好记性不如烂笔头

1.ActionResult Return Type

Most action methods return an instance of a class that derives from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing. For example, the most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.

You can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream.

The following table shows the built-in action result types and the action helper methods that return them.

Action Result

Helper Method

Description

ViewResult

View

Renders a view as a Web page.

PartialViewResult

PartialView

Renders a partial view, which defines a section of a view that can be rendered inside another view.

RedirectResult

Redirect

Redirects to another action method by using its URL.

RedirectToRouteResult

RedirectToAction

RedirectToRoute

Redirects to another action method.

ContentResult

Content

Returns a user-defined content type.

JsonResult

Json

Returns a serialized JSON object.

JavaScriptResult

JavaScript

Returns a script that can be executed on the client.

FileResult

File

Returns binary output to write to the response.

EmptyResult

(None)

Represents a return value that is used if the action method must return a null result (void).

  MVC学习笔记

    Content——存放应用需要的一些资源文件,如图片、CSS等。
      Controllers——存放控制器类。
      Models——存放业务模型组件。
      Scripts——存放JavaScript脚本文件。
      Views——存放视图。

一些ASP.NET MVC可用的模板引擎:

Brail
NHaml
NVelocity
XSLT
上面的4个都包括在MVC Contrib中了

还有我比较喜欢的一个:Spark

一个用于ASP.NET MVC的业务实体验证的开源项目:xVal,介绍见xVal - a validation framework for ASP.NET MVC、xVal 0.8 (Beta) Now Released。

asp.net mvc相关开源项目推荐 asp.net mvc ctp版本发布不到一个月时间,在社区出现了丛多的优秀开源项目,社区的活跃性非常高哦,前一段时间园子里也引发了MVC和WebForm的讨论,现在给各位推荐几个相关的优秀项目,这些项目是MVC的价值的最佳证明。

1、 Asp.net mvc 和Castle 的MonoRail项目有着直接的渊源关系,第一个是Castle的Monorail项目,网站地址: http://www.castleproject.org/ ,建议读读这篇文章:MS MVC框架漩涡中的MonoRail未来。 我也在使用WIKI整理castle 的文档:http://www.openbeta.cn/ioc.ashx

2、 NHaml --ASP.NET MVC View Engine,这是一个Ruby的Haml的版本,对于熟悉Ruby开发的同学一定很熟悉,项目网站:http://haml.hamptoncatlin.com/。具体介绍可看这篇文章:http://andrewpeters.net/2007/12/19/introducing-nhaml-an-aspnet-mvc-view-engine

3、 Mvp.Xml :Mvp.Xml是一组 Xml 的 Library,Mvp.Xml除了可以让 System.Xml 更好用之外,最主要的是他提早实做许多Microsoft还来不及包在 Framework 里面的 XML 标准的类别,例如:EXSLT、XML Base、XInclude、XPointer...等等。Mvp.Xml结合System。XML和System.Xml.Linq 就实现了XsltViewEngine视图引擎,项目网站:http://www.codeplex.com/MVPXML

4、 mvccontrib:已经有好几个的.NET IOC应用框架:Castle Windsor 、StructureMap 、Spring.NET 、Ninject、以及微软Enterprise Library 4.0将支持依赖注入 。这个项目就是整合这些IoC框架以及视图引擎,包括Castle MonoRail所用的NVelocityView视图引擎、NHamlView视图引擎、XsltViewEngine视图引擎等等,如果你有精力和激情,可参与到这个项目的开发,项目地址:http://code.google.com/p/mvccontrib/ 。可阅读如下文章:IoC Container Support for Windsor, StructureMap and Spring.Net,Jeffrey Palermo在博客里讨论了建立在ASP.NET MVC框架之上的新MVCContrib开源项目,它现在包括了对大多数流行的 .NET IoC工具的Inversion of Control(控制反转)容器的支持

5、Kigg - A Digg like application developed in ASP.NET MVC :http://weblogs.asp.net/rashid/archive/2008/02/18/kigg-a-digg-like-application-developed-in-asp-net-mvc.aspx

MVC Contrib已提供4种备选的视图引擎
http://www.infoq.com/cn/news/2008/02/mvc-contrib-view-engines

Using jQuery to add some ajax magic to the ASP.NET mvc framework
http://code.google.com/p/jquerymvc/

ASP.NET MVC CAPTCHA:ASP.NET MVC中实现CAPTCHA的方法
http://code.google.com/p/coderjournal/
http://www.coderjournal.com/2008/03/actionfilterattribute-aspnet-mvc-captcha/

ASP.NET MVC CSS Sprite
http://weblogs.asp.net/zowens/archive/2008/04/27/asp-net-mvc-css-sprite.aspx


6、S#arp Architecture: ASP.NET MVC with NHibernate

     http://code.google.com/p/sharp-architecture/

7、 MVC Storefront.

      http://www.codeplex.com/mvcsamples

      RESTful routing in ASP.NET MVC

 8、MvcWebAPI

    http://code.msdn.microsoft.com/MvcWebAPI

14、ASP.NET MVC XForms is a simple, strongly-typed, extensible UI framework based on the W3C XForms spec. It provides a base set of form controls that allow updating of any complex model object, even complex nested lists. It uses clean, semantic HTML and a fluent, lamba-based API

http://www.codeplex.com/mvcxforms

15 A heavily opinionated, highly conventional ASP.NET-based MVC extension framework

 http://code.google.com/p/opinionatedmvc/


9、MVC Applications

    http://www.codeplex.com/mvcapps

10、ASP.NET MVC Flexigrid sample

  http://www.webplicity.net/flexigrid/

  http://www.codeproject.com/KB/aspnet/MVCFlexigrid.aspx


  ASP.NET MVC : Create Toolbar Control http://www.codeproject.com/KB/aspnet/MVCToolBarControl.aspx

 

11、CarTrackr - Sample ASP.NET MVC application

    http://www.codeplex.com/CarTrackr

12、FastMVC: Compress and Cache Dynamic and Static requests

http://code.msdn.microsoft.com/fastmvc

13、Rick URL Shortening Service   http://www.codeplex.com/rick


      http://www.codeproject.com/KB/aspnet/RESTful_MVC_Sample.aspx

14  A heavily opinionated, highly conventional ASP.NET-based MVC extension framework  http://code.google.com/p/opinionatedmvc/

15 Asp.net MVC Open Source Site - prsync.com  http://prsync.com/

   MVC可以看作是表现层与业务逻辑层的协作方式。
三层架构是表现层,业务逻辑,数据访问层。
Model不是数据访问层,而往往是横跨所有层次的对象。
不过也有架构(师)喜欢在层与层之间把实体对象mapping来mapping去,我觉得很丑恶很麻烦
-----------Jeffrey Zhao


它首先继承了Controller类。Controller类是ASP.NET MVC框架中提供的一个控制器积累,所有我们自定义的控制器类都要继承此基类。然后这个类中有一个Index方法,返回值类型是ActionResult。


控制器类可以说是ASP.NET MVC的核心类,因为它将处理一切请求,并处理所有页面转发等表示逻辑,这也是使用了ASP.NET MVC后与传统ASP.NET应用最大的差别。在传统模式下,一个用户请求的url将对应一个aspx文件,而在ASP.NET MVC下,一个用户请求对应某个控制器类中的一个方法,而这个方法,就叫做一个Action。至于如何对应的,则是通过对url的解析。
      例如,在传统情况下,http://localhost/Default.aspx表示请求网站根目录下的Default.aspx文件。而现在,url可能变成了这种样子:http://localhost/Home/Index。这个意思就是,请求名叫HomeController控制器类下的Index方法。一般地,默认情况下,请求url的格式为http://localhost/{ControllerName}/{ActionName}。其中{ControllerName}是控制器类名“Controller”前的部分,{ActionName}就是方法名。
      当然,这种映射规则是可以更改的,而且请求Action时也可以传递参数,但这些都是后话,以后再慢慢讨论。
  下面再深入Index方法,看看这个Action都做了什么。它首先调用了业务逻辑组件(当然,是Mock的),然后将GetAll返回的公告分类数据赋予ViewData["Category"],最后调用View()方法返回一个ActionResult。ViewData是什么呢?你可以把他理解成一个关联数组,它保存需要传给视图的数据。而View是Controller类的一个方法,它返回一个ActionResult实例。这样说可能有点抽象,其实直观就是将某个视图(一般就是一个aspx文件)呈现到浏览器中。那么如何知道呈现哪一个视图呢?默认情况下,View方法会到网站的Views文件夹下的与控制器类同名的文件夹下寻找与Action方法同名的视图。例如,HomeController的Index方法就会寻找Views/Home/Index.aspx,如果找不到,就会到Shared下寻找,再找不到就报错了。当然,你也可以给View方法传递一个字符串参数,表示视图名称。

实现视图
      上文说到,当请求http://localhost/Home/Index时,HomeController的Index方法会被调用,而Index方法最后要呈现Views/Home/Index.aspx视图,所以,我们要在Views文件夹下建立一个Home文件夹,然后再新建一个Index.aspx视图。如果您使用的是VS2008 SP1,那么建立视图非常方便,只要在Home文件夹下右键单击,选Add--->View,然后指定视图名就可以了。如果不是SP1的,就新建一个Item,类型选择“MVC View Page”。建立好的视图其实就是一个aspx页面,但是其继承了View。这也是一个基类,所有视图需要继承它。

 

原文地址:https://www.cnblogs.com/sendling/p/1425429.html