惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园_首页
H
Help Net Security
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
GbyAI
GbyAI
Scott Helme
Scott Helme
D
Docker
Hacker News: Ask HN
Hacker News: Ask HN
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
C
Cisco Blogs
The Hacker News
The Hacker News
F
Full Disclosure
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
G
Google Developers Blog
量子位
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
P
Palo Alto Networks Blog
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Schneier on Security
Schneier on Security
The Register - Security
The Register - Security
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
N
News and Events Feed by Topic
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
V
V2EX
爱范儿
爱范儿

博客园 - everx

CruiseControl.net - 找到的一些文档。分享出来 【摘抄】回归测试(Regression Test) 那就来说说ASP.NET MVC中的Routing吧 ASP.NET MVC - View ASP.NET MVC - Controller(part Ⅰ) ASP.NET MVC - Model 学一下ASP.NET MVC C# 3.0的新特性 jQuery学习笔记(八) jQuery 学习笔记(七) SilverLight学习之路(四) Silverlight学习之路(三) - everx - 博客园 JQuery学习笔记(六) JQuery学习笔记(五) 初次使用log4net Silverlight学习之路(二) JQuery学习笔记(四) 播下silverlight的种子 JQuery学习笔记(三)
ASP.NET MVC - Controller(part Ⅱ)
everx · 2009-10-20 · via 博客园 - everx

接着上次说的,这次继续说一下Controller的其他功能。

首先是IModelBinder接口。它只有一个方法:

public interface IModelBinder {
        
object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);
    }

 这个方法可以将用户输入(Form,Route,Querystring)转换成我们自定义的类。ASP.NET MVC自身自带了一个Model Binder:DefaultModelBinder。它可以将输入值转换成.net 的原生类型,甚至是IList类型。但是如果我们需要将输入转换成自定义的类型的话,就需要进行扩展了。具体的用法,现在我还不是很清楚,还需要继续探索。但是可以知道的是,一旦自定义了一个Model Binder,就必须在Globle.asax中进行注册才行。

然后,就是说一下Controller的filter。filter的功能就是在ASP.NET MVC的请求过程中,插入自己的逻辑代码。其中,有4个接口是用于filter功能的:IActionFilter、IResultFilter、IAuthorizationFilter、IExceptionFilter。

public interface IActionFilter {
        
void OnActionExecuting(ActionExecutingContext filterContext);
        
void OnActionExecuted(ActionExecutedContext filterContext);
    }
public interface IAuthorizationFilter {
        
void OnAuthorization(AuthorizationContext filterContext);
    }
public interface IResultFilter {
        
void OnResultExecuting(ResultExecutingContext filterContext);
        
void OnResultExecuted(ResultExecutedContext filterContext);
    }
public interface IExceptionFilter {
        
void OnException(ExceptionContext filterContext);
    }

 可以看出,这些方法有一点像是WEBFORM中熟悉的那些页面生命周期事件。这些方法的执行顺序大概是:OnAuthorization -> OnActionExecuting -> OnActionExecuted -> OnResultExecuting -> OnResultExecuted -> OnException。当然最后那个OnException是不定的,因为一旦出现异常,程序就会立即执行这个方法。

Controller类,已经实现了这几个接口,所以我们就可以通过覆盖实现这几个方法来插入自定义逻辑。但是,还有一种更好的方法,就是使用FilterAttrbute,来插入自定义逻辑。这样做有一个很大的好处,那就是,我们可以针对某个Action进行逻辑操作,而不必操作整个Controller的所有Action。MVC默认给我们提供了几个Attribute。我们也可以编写我们自己的Attribute。写法大概就是,1.继承FilterAttribute 2.实现相应的接口 (上面所说的那4个)。

 关于Controller就说到这了,还有很多需要继续深入的地方。后面在慢慢补充吧。