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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
U
Unit 42
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
G
Google Developers Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Jina AI
Jina AI
量子位
宝玉的分享
宝玉的分享
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
美团技术团队
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
博客园 - 司徒正美
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Privacy International News Feed
G
GRAHAM CLULEY
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
V2EX - 技术
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就说到这了,还有很多需要继续深入的地方。后面在慢慢补充吧。