Cos’è? Introducing Asp.Net • La risposta Microsoft alle esigenze del server side (leggi JSP) • L’evoluzione (rivoluzione) di ASP Ing. Gabriele Zannoni • La tecnologia per la scrittura di pagine gabriele.zannoni@unibo.it dinamiche utilizzando il .Net Framework Introducing Asp.Net 1 Introducing Asp.Net 2 Come funziona? come funziona • Si appoggia su IIS (Internet Information Server) � Web client (browser) Server di casa Microsoft • E’ un’estensione ad IIS: tutte le richieste di pagine con ASP.NET request response Counter.aspx una particolare estensione vengono passate al “filtro” ("Counter.aspx") (*.html) ISAPI di Asp.Net preprocessor, compiler "Counter.aspx" • Tale filtro è in grado di compilare le pagine Asp.Net in server page class (IIS) assembly e di mandarli in esecuzione loader • Asp.Net è sostanzialmente un framework estendibile page object *.html • Una pagina Asp.Net è elaborata da un HttpModule particolare: è possibile scrivere HttpModule personalizzati (leggi Servlet) .NET framework Introducing Asp.Net 3 Introducing Asp.Net 4 counter.aspx counter.aspx in script tags � <%@ Page Language="C#" %> � <%@ Page Language="C#" %> � <%@ Import Namespace="System.IO" %> � <%@ Import Namespace="System.IO" %> � <html> � <html> <head> <title>Page counter</title> </head> <head> � � <body> <title>Page counter</title> � � <h1>Welcome</h1> <script Language="C#" Runat="Server" > � � You are visitor number <% int CounterValue() { � � FileStream s = new FileStream("c:\\Data\\Counter.dat", FileStream s = new FileStream("c:\\Data\\Counter.dat", � � FileMode.OpenOrCreate); FileMode.OpenOrCreate); int n; ... � � try { n = r.ReadInt32(); � � BinaryReader r = new BinaryReader(s); n++; � � n = r.ReadInt32(); ... � � } catch { n = 0; } // if the file is empty return n; � � n++; } � � s.Seek(0, SeekOrigin.Begin); </script> � � BinaryWriter w = new BinaryWriter(s); </head> � � w.Write(n); s.Close(); <body> � � Response.Write(n); <h1>Welcome</h1> � � %> ! You are visitor number <% = CounterValue()%> ! � � </body> </body> � � � </html> � </html> Introducing Asp.Net 5 Introducing Asp.Net 6
counter.aspx in “Code behind” Comunque… � namespace ASP { � using System.IO; � ... � <%@ Page Language="C#" Inherits="CounterPage" Src="CounterPage.cs" %> System.Web.UI.Page � public class Counter_aspx : CounterPage { � private static bool __initialized = false; � <html> � private static ArrayList __fileDependencies; <head> <title>Page counter</title> </head> � public Counter_aspx() { � � ArrayList dependencies; <body> � Counter.aspx � if ((__initialized == false)) { ... } <h1>Welcome</h1> � � } You are visitor number <%=CounterValue()%> ! � public override string TemplateSourceDirectory { � � get { return "/Samples"; } </body> � aspx page Counter.aspx � } � </html> � private void __BuildControlTree(Control __ctrl) { <%@ Page Language="C#"%> <html> � __ctrl.SetRenderMethodDelegate(new counter.aspx <body> RenderMethod(this.__Render__control1)); � using System.IO; ... <%= ... %>... � } </body> ... CounterPage.cs � private void __Render__control1(HtmlTextWriter __output, </html> Control parameterContainer) { � public class CounterPage : System.Web.UI.Page { System.Web.UI.Page � __output.Write("\r\n<html>\r\n\t<head> <title>Page counter</title> public int CounterValue () { � </head>\r\n\t<body>\r\n\t\t" + Code behind CounterPage.cs FileStream s = new FileStream("c:\\Data\\Counter.dat", "<h1>Welcome</h1>\r\n\t\tYou are visitor number "); � � FileMode.OpenOrCreate); public class CounterPage : System.Web.UI.Page { � __output.Write(CounterValue()); CounterPage public int CounterValue () { � __output.Write(" !\r\n\t</body>\r\n</html>\r\n"); ... ... � � } CounterValue() } n = r.ReadInt32(); � } � protected override void FrameworkInitialize() { � __BuildControlTree(this); n++; � � this.FileDependencies = __fileDependencies; Counter.aspx ... aspx page � � this.EnableViewStateMac = true; this.Request.ValidateInput(); return n; <%@ Page ... Inherits="CounterPage"%> � <html> � } } <body> � � ... ... <%=CounterValue()%>... � } � } </body> ... </html> � } Introducing Asp.Net 7 Introducing Asp.Net 8 Class Page Class HttpRequest public class Page : TemplateControl { //--- properties IsValid UserHostName public class HttpRequest { public ValidatorCollection Validators { get; } true, if none of the validators domain name of the client public string UserHostName { get; } public bool IsValid { get; } on the page reported an error public string UserHostAddress { get; } public bool IsPostBack { get; } UserHostAddress public string HttpMethod { get; } public virtual string TemplateSourceDirectory { get; } IsPostBack IP number of the client public HttpBrowserCapabilities Browser { get; } public HttpApplicationState Application { get; } true, if the page was sent to the public NameValueCollection Form { get; } public virtual HttpSessionState Session { get; } public NameValueCollection QueryString { get; } server in a round trip. If the page public HttpRequest Request { get; } public NameValueCollection Cookies { get; } was requested for the first time public HttpResponse Response { get; } public NameValueCollection ServerVariables { get; } ... IsPostBack == false ... //--- methods } TemplateSourceDirectory public string MapPath (string virtualPath); public virtual void Validate (); current virtual directory, ... e.g. "/Samples" <body> address = 127.0.0.1 } <%= "address = " + Request.UserHostAddress %><br> method = GET Application and Session <%= "method = " + Request.HttpMethod %><br> browser = IE application state and session state <%= "browser = " + Request.Browser.Browser %><br> version = 6.0 MapPath (virtPath) <%= "version = " + Request.Browser.Version %><br> supports JS = True maps the virtual directory to the physical one <%= "supports JS = " + Request.Browser.JavaScript %><br> server = Microsoft-IIS/5.0 Request und Response <%= "server = " + Request.ServerVariables["SERVER_SOFTWARE"] %> Validate () HTTP request and HTTP response </body> starts all validators on the page Introducing Asp.Net 9 Introducing Asp.Net 10 Class HttpResponse Controlli Visuali public class HttpResponse { ContentType //--- properties MIME type (e.g. text/html) public string ContentType { get; set; } public TextWriter Output { get; } Output public int StatusCode { get; set; } HTML response stream; can be public HttpCookieCollection Cookies { get; set; } ... written to with Write Control //--- methods StatusCode ID public void Write (string s); // various overloaded versions Page e.g. 200 for "ok" or public void Redirect (string newURL); Visible 404 for "page not found" ... } Test1.aspx ... WebControl TemplateControl <form Runat="server"> Font Name: <asp:TextBox ID="name" Runat="server" /> Width <asp: Button Text="Send" OnClick="DoClick" Runat="server" /> Height </form> void DoClick (object sender, EventArgs e) { Response.Redirect("Welcome.aspx?name=" + name.Text); ... ... } Button TextBox Label Page UserControl Text Text Text Request Welcome.aspx Rows Response Welcome <%= Request.QueryString["name"] %> ! Columns IsPostBack Introducing Asp.Net 11 Introducing Asp.Net 12
Recommend
More recommend