All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#中TextBox控件应用实例
PHP的特点与市场情况_php资料_编程技术
用ASP.NET代码实现备份SQL,Server数据库_[Asp.Net教程]
ASP.NET实现简单的验证码_[Asp.Net教程]
smarty技术学习,修改php模板的一点体会_[PHP教程]
php和explode_php资料_编程技术
将Web页转换为ASP.NET 2.0用户控件
sqlserver2000定时自动备份的实现_mssql学习_编程技术
c#中域和属性的概念
xmlHTTP,xmlDOC,与,C#中DataSet的结合,实现AJAX简单示例_.net资料_编程技术
Reading number is top 10 pictures
Beauty ZhiHuiLin2
A beautiful girl to bud1
A man's favorite things1
Female model behind the bitterness, often being overcharged3
Tie a large font of mouse
美女浴室写真3
美女当网吧管理员的悲剧
Get girl by your hand
From China fortress sora aoi2
中国文革时期的色情图片2
Download software ranking
C++编程教程第三版
jBuilder2006
Unix video tutorial5
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Such love down(擒爱记)
White deer villiage
Unix video tutorial8
Boxer vs Yellow4
matrix3
matrix1
归海一刀 published in(发表于) 2014/1/30 0:52:35 Edit(编辑)
ASP.NET,MVC,Framework体验(4):控制器_[Asp.Net教程]

ASP.NET,MVC,Framework体验(4):控制器_[Asp.Net教程]

ASP.NET MVC Framework体验(4):控制器_[Asp.Net教程]


概述


在MVC中,Controller用来处理和回应用户的交互,选择使用哪个View来进行显示,需要往视图中传递什么样的视图数据等。ASP.NET MVC Framework中提供了IController接口和Controller基类两种类型,其中在Controller提供了一些MVC中常用的处理,如定位正确的action并执行、为action方法参数赋值、处理执行过程中的错误、提供默认的WebFormViewFactory呈现页面。IController只是提供了一个控制器的接口,如果用户想自定义一个控制器的话,可以实现IController,它的定义如下:

public interface IController
{
void Execute(ControllerContext controllerContext);
}

定义控制器和action


在前面三篇的例子中,我们已经定义过了控制器,只要继承于Controller就可以了:

public class BlogController : Controller
{
[ControllerAction]
public void Index()
{
BlogRepository repository = new BlogRepository();
List posts = repository.GetAll();
RenderView("Index", posts);
}
[ControllerAction]
public void New()
{
RenderView("New");
}
}
通过ControllerAction特性来指定一个方法为action,ControllerAction的定义非常简单:
[AttributeUsage(AttributeTargets.Method)]
public sealed class ControllerActionAttribute : Attribute
{
public ControllerActionAttribute();
}

使用强类型传递ViewData


通过前面的一些示例,已经看到了一些示例如何从控制器传递视图数据给View,在Controller中,传递视图数据到View,我们可以有两种方式选择,其中一种是使用强类型来传递视图数据,如下示例代码:

[ControllerAction]
public void Index()
{
BlogRepository repository = new BlogRepository();
List posts = repository.GetAll();
RenderView("Index", posts);
}

有朋友在回复中提到,如果想传递多个Model或者集合数据到View,该如何传递?这里需要再定义一个类型:

public class HomeViewData
{
public List Posts
{
get; set;
}
public List Categories
{
get; set;
}
}

然后在控制器中可以这样进行传递数据:

[ControllerAction]
public void Index()
{
BlogRepository repository = new BlogRepository();
List posts = repository.GetAll();
List categories = repository.GetAllCategory();
HomeViewData viewData = new HomeViewData();
viewData.Posts = posts;
viewData.Categories = categories;
RenderView("Index", viewData);
}

使用强类型类来传递视图数据,有如下好处(来自于Scrottgu):


1.避免使用字符串来查询对象,得到对你的控制器和视图代码的编译时检查


2.避免需要在使用象C#这样的强类型语言中明确转换ViewData对象字典中的值


3.在你的视图网页的标识文件以及后台代码文件中得到你的ViewData对象的自动代码intellisense


4.可以使用代码重构工具来帮助自动化对整个应用和单元测试代码库的改动


使用ViewData字典来传递数据


在Controller基类中,有一个这样的字典定义:

public IDictionary ViewData { get; }

这样我们可以直接把视图数据通过ViewData字段来传递,如下示例代码:

[ControllerAction]
public void Index()
{
BlogRepository repository = new BlogRepository();
List posts = repository.GetAll();
List categories = repository.GetAllCategory();
ViewData["posts"] = posts;
ViewData["categories"] = categories;
RenderView("Index");
}

在试图中,可以这样来获取视图数据:


<%foreach (Post post in (ViewData["posts"] as List))
{ %>
Title:<%=Html.Encode(post.Title) %>

Author:<%=Html.Encode(post.Author) %>

PubDate:<%=Html.Encode(post.PubDate.ToShortDateString()) %>

Content:<%=Html.Encode(post.Description) %>

<%=Html.ActionLink("Edit", new {action="Edit", Id=post.Id })%>


<% } %>

处理未知的Action


Controller类中包含了一个HandlerUnknownAction的方法:

protected internal virtual void HandleUnknownAction(string actionName);
它用来处理一些未知的Action,默认情况下将返回HTTP 404错误,如果想自定义该处理,可以覆写该方法:
[ControllerAction]
protected override void HandleUnknownAction(string actionName)
{
if (ShouldShowSearch(action) == true)
{
RedirectToAction("search", new { query = action });
return;
}
base.HandleUnknownAction(actionName);
}

它用来处理当出现未知的Action时,跳转向Search Action。


结束语


在本篇文章中,我们介绍了ASP.NET MVC Framework中的Controller,包括如何定义Controller及Action,通过强类型和视图数据字典来传递视图数据,以及自定义处理未知的Action等内容,希望对您有所帮助。最后,插播一条小广告:我在博客园社区中建立了一个Web技术联盟小组,欢迎大家加入:http://space.cnblogs.com/group/webdev/

作者:TerryLee
出处:http://terrylee.cnblogs.com






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.