这期内容当中小编将会给大家带来有关C#中如何使用对象,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
一、HttpModule
这个对象我们经常用来进行统一的权限判断、日志等处理。
例子代码:
publicclassMyModule:IHttpModule { publicvoidInit(HttpApplicationapplication) { application.BeginRequest+=newEventHandler(application_BeginRequest); } voidapplication_BeginRequest(objectsender,EventArgse) { ((HttpApplication)sender).Response.Write("Copyright@Gspring<br/>"); } publicvoidDispose() { } }
在Init方法中可以注册很多application的事件,我们的例子就是在开始请求的时候加入自己的代码,将版权声明加到页面的头部
二、HttpHandler
这个对象经常用来加入特殊的后缀所对应的处理程序,比如可以限制.doc的文件只能给某个权限的人访问。
Asp.Net中的Page类就是一个IHttpHandler的实现
例子代码:
publicclassMyHandler:IHttpHandler { publicvoidProcessRequest(HttpContextctx) { ctx.Response.Write("Copyright@Gspring<br/>"); } publicboolIsReusable { get{returntrue;} } }
这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么所有的aspx页面都不能正常运行了
三、HttpHandlerFactory
这个对象也可以用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更加强大,在系统的web.config中就是通过注册HttpHandlerFactory来实现aspx页面的访问的。
HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。
publicclassMyHandlerFactory:IHttpHandlerFactory { publicIHttpHandlerGetHandler(HttpContextcontext,stringrequestType,stringurl,stringpathTranslated) { PageHandlerFactoryfactory=(PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory),true); IHttpHandlerhandler=factory.GetHandler(context,requestType,url,pathTranslated); //执行一些其它操作 Execute(handler); returnhandler; } privatevoidExecute(IHttpHandlerhandler) { if(handlerisPage) { //可以直接对Page对象进行操作 ((Page)handler).PreLoad+=newEventHandler(MyHandlerFactory_PreLoad); } } voidMyHandlerFactory_PreLoad(objectsender,EventArgse) { ((Page)sender).Response.Write("Copyright@Gspring<br/>"); } publicvoidReleaseHandler(IHttpHandlerhandler) { } }
上述就是小编为大家分享的C#中如何使用对象了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。