ASP网站制作之显现体系日记(ASP+)
ASP由于使用了COM组件所以它会变的十分强大,但是这样的强大由于WindowsNT系统最初的设计问题而会引发大量的安全问题。只要在这样的组件或是操作中一不注意,哪么外部攻击就可以取得相当高的权限而导致网站瘫痪或者数据丢失;asp+|显现Writtenby:ChristophWilleTranslatedby:BernhardSpuida
Firstpublished:8/11/2000
UnderWindows2000(orNT)theEventLogisaboutthemostimportantsourceofinformationforthe
administratorbecausealleventsthatoccurredareloggedthere-fromsuccesstocatastrophicalfailure.
Andasitissoimportant,whatwouldbemoreobviousthantomakeitaccessibleviatheweb?
TheEventViewershouldbefamiliartonearlyeveryone(seepicture).InthisarticleIwilldemonstrate
howthelistofentriescanbeemulatedveryelegantlyusingASP.NETandthe.NETFrameworkSDK.Asan
exerciseforthereaderIwillleavetheconstructionofapageforthefulldetailsofanentry.
TheuseofthesourcecodeinthisarticlerequirestheMicrosoft.NETFrameworkSDKinstalledona
Webserver.IalsopresumethatthereaderisfamiliartosomedegreewiththeC#programminglanguage.
TheBruteForceMethod
Whenwehavetobequickanddirty,knowledgefromthedaysofASPcanverywellbeusedtogeneratea
listofevents(Evenwithatable,thoughthisexampledoesntdothat).Thenameoftheprogramisthe
nameofthegame:simple.aspx.
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Diagnostics"%>
<%
EventLogaLog=newEventLog();
aLog.Log="System";
aLog.MachineName=".";//LokaleMaschine
stringstrImage="";//IconfürdasEvent
Response.Write("<p>Thereare"+aLog.Entries.Count+
"entriesintheSystemeventlog.</p>");
foreach(EventLogEntryentryinaLog.Entries)
{
switch(entry.EntryType)
{
caseEventLogEntryType.Warning:
strImage="warning.png";
break;
caseEventLogEntryType.Error:
strImage="error.png";
break;
default:
strImage="info.png";
break;
}
Response.Write("<imgsrc=""+strImage+"">|");
Response.Write(entry.TimeGenerated.ToString()+"|");
Response.Write(entry.Source+"|");
Response.Write(entry.EventID.ToString()+"
");
}
%>
TheclassesfortheEventLogarefoundintheNamespaceSystem.Diagnosticswhichisboundinatthe
beginningofthepage.Openingthelogisinitselfstraightforward:createanewEventLogobject,specify
theLogandtheMachineName("."isthelocalmachine).AndwerereadytoreadfromtheEventLog.
Thisisdoneinaforeachloop.Tomakethelistinglessunimaginative,Iputthecorrecticonbeforeeach
entry.Bytheway,thelistingofentriesisthereverseoftheusualorderintheEventViewer:here,the
oldestentriesarelistedfirst.
MoreelegantwiththeDataGrid
ASP.NETcomeswithmanyinnovations,especiallyfordisplayingdata.Andthegoodpartaboutthatisthat
thedatadoesnotalwayshavetocomeoutofadatabase.ThisalsoistruefortheDataGridWebControl
which,asthenamesays,createsatable(grid)outofthedata.Theonlyrequirementisthatthedata
sourcesupportstheICollectioninterface-andtheEntriesCollectionoftheEventLogdoesjustthat.
Thefollowingsourcecode(datagrid.aspx)showshowsimpleusingtheDataGridis:
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Diagnostics"%>
<scriptlanguage="C#"runat="server">
voidPage_Load(Objectsender,EventArgse)
{
EventLogaLog=newEventLog();
aLog.Log="System";
aLog.MachineName=".";
LogGrid.DataSource=aLog.Entries;
LogGrid.DataBind();
}
</script>
<bodybgcolor="#ffffff">
<h3>SystemEventLog</h3>
<formrunat="server">
<ASP:DataGridid="LogGrid"runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
/>
</form>
</body>
</html>
TheDataGridControlonlycontainsformattinginstructions,nothingelse.TheGridis</p>优点:简单易学、开发速度快、有很多年“历史”,能找到非常多别人做好的程序来用、配合activeX功能强大,很多php做不到的asp+activeX能做到,例如银行安全控件 封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变,业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高。 Server:这个表示的服务器,操作服务器的一些东西使用这个,如Server.Mappath转换服务器路径,Server.CreateObject实例化一个组件 我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。 另外因为asp需要使用组件,所以了解一点组件的知识(ADODB也是组件) 代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。 我就感觉到ASP和一些常用的数据库编程以及软件工程方面的思想是非常重要的。我现在也在尝试自己做网页,这其中就用到了ASP,我想它的作用是可想而知的。 还有如何才能在最短的时间内学完?我每天可以有效学习2小时,双休日4小时。 学习ASP其实应该上升到如何学习程序设计这种境界,其实学习程序设计又是接受一种编程思想。比如ASP如何学习,你也许在以前的学习中碰到过。以下我仔细给你说几点: 你可以通过继承已有的对象最大限度保护你以前的投资。并且C#和C++、Java一样提供了完善的调试/纠错体系。
页:
[1]