Bài giảng Lập trình Asp.Net - Phần 1: Giới thiệu MS.Net, ASP.Net - Nguyễn Hà Giang

1. MS. NET Framework & C# Nền tảng MS .NET Framework Ngôn ngữ chính của .NET: C# 2. ASP.NET Tổng quan về công nghệ ASP.NET Lịch sử phát triển Đặc tính cơ bản MS .NET Framework Chương trình nền tảng cho cho công nghệ .NET Cung cấp tập hợp class library thường dùng Quản lý sự thực thi của các chương trình .NET

ppt51 trang | Chia sẻ: candy98 | Lượt xem: 475 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Bài giảng Lập trình Asp.Net - Phần 1: Giới thiệu MS.Net, ASP.Net - Nguyễn Hà Giang, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Giới thiệu MS.NET ASP.NETTrình bày: ThS Nguyễn Hà GiangNội dungMS. NET Framework & C#Nền tảng MS .NET FrameworkNgôn ngữ chính của .NET: C#ASP.NETTổng quan về công nghệ ASP.NETLịch sử phát triểnĐặc tính cơ bản12/3/20202MS .NET Framework12/3/20203MS .NET FrameworkChương trình nền tảng cho cho công nghệ .NETCung cấp tập hợp class library thường dùngQuản lý sự thực thi của các chương trình .NETCommon Language RuntimeBase Framework ClassesData and XML ClassesCustom ClassesC# VB.NET J# C++ XML Web ServiceWeb FormsWindows FormsASP.NET12/3/2020412/3/2020Nguyen Ha Giang 5.NET Framework - history.NET 1.1 (Apr-2003)VS .NET 2003Default: Server 2003.NET 3.5 (Nov-2007)VS.NET 2008Default: Windows 7.NET 3.0 (Nov-2006)Default: Windows Vista, Server 2008.NET 2.0 (Nov-2005)VS.NET 2005 12/3/20206.NET 4.5!.NET Framework – Kiến trúcCommon Language Infrastructure (CLI)Provide a language-neutral platform for application development and execution 12/3/20207Features of .NET AppChạy trên nền (.NET Framework)Mã nguồn được biên dịch qua MSILMSIL được thông dịch qua mã máy lúc thực thi nhờ vào CLRĐộc lập nền tảngVề lý thuyết có thể chạy trên mọi nền!12/3/20208Features of .NET AppMS ILCLRC++ CompilerC# CompilerJ# .NET CompilerCT C#.NETCT VB.NETCT C++.NET.NET FrameworkCT J#.NETVB .NET Compiler101101010110101011010Programmer12/3/20209.NET Framework - CLRTheo quan điểm của người lập trình, .NET có thể hiểu như môi trường thực thi mới và thư viện lớp cơ sở cải tiến.Môi trường thực thi là: Common Language Runtime - CLRVai trò chính CLR: locate, load, manage .NET typesCLR còn quản lý những phần ở mức thấp như: memory management, security check12/3/202010.NET Framework - CTSCommon Type System (CTS): Mục đích hỗ trợ thực thi chéo ngôn ngữĐịnh nghĩa kiểu DL tiền định và có sẵn trong IL:Tất cả ngôn ngữ .NET sẽ được sinh ra mã cuối trên cơ sở kiểu dữ liệu này!IntegerInt32intVB.NETILC#12/3/202011MS Intermediate Language Intermediate Language Abstracted assembly language Ý tưởng về mã Java byteMã cấp thấp cú pháp đơn giản  quá trình dịch sang mã máy nhanh hơn!CLR chuyển IL thành mã máy lúc runtimeSự chuyển này gọi là Just – In – Time Compilation hay JIT compiling12/3/202012CLR- Compilation 13C++, C#, VB or any .NET languagecsc.exe or vbc.exe...Compiler.NET AssemblyDLL or EXESource Code12/3/2020AssemblyManaged ModulesChứa IL code để thực thiChứa resource, metadataPortable Executable (PE)Process assembly (EXE)Library assembly (DLL)14.NET AssemblyDLLEXE12/3/2020Managed Code15Ứng dụng được viết để sử dụng trong .NET FrameworkUD khác thì gọi là Unmanaged code“Managed”: do chạy dưới sự giám sát của cơ chế thực thi (CLR)Windows(OS khác)Legacy Software(unmanaged code)Common Language RuntimeManaged Executable12/3/2020Nguyen Ha Giang - 2009Garbage collection16class myClass{ void Method(){ variable v1; variable v2; do{ . }}BECADManaged HeapA và D sẽ bị xoá do ko còn tham chiếu hay truy cập tới12/3/2020Garbage collection17GC xuất hiện không định trước, do CRL gọi.GC thực hiện việc tìm kiếm những đối tượng trong managed heap, xoá nếu ko còn tham chiếu tới.Có thể gọi GC một cách tường minh12/3/2020Namespace18Tránh xung đột tênCho phép phân cấpTruy cập đầy đủ qua tênTất cả data type có tiếp đầu ngữ là tên namespaceNamespace CS1Class AClass BClass CNamespace CS2Class AClass BClass CCS1.ACS2.A12/3/2020Khác nhau!Namespace19Hầu hết các lớp cơ sở chung của .NET đều thuộc namespace SystemLớp CS Array thuộc System → System.Array.NET đề nghị tất cả kiểu do user định nghĩa phải nằm trong 1 namespaceCustomerPersonelCompanyNameTên lớpTên namespace12/3/2020Namespace20Nên phân thành 2 cấp:Công nghệ Gói phần mềm chức năngCustomerPersonelCompanyNameTên lớpTên namespaceSaleServicesManagingServices12/3/2020Các lớp .NET Framework21Thư viện lớp cơ sở .NET là managed codeĐược xây dựng từ ngôn ngữ C#!Khá đa dạng & đầy đủ:Cho phép kế thừa để phát triển UDBao bọc tất cả các hàm APIDễ sử dụng (khác với VC++ trước đây)12/3/2020Các lớp .NET Framework22Các lớp .NET bao gồm các vấn đềĐặc tính lõi cung cấp IL, kiểu dữ liệu trong CTSHỗ trợ Win GUI và controlWebForm (ASP.NET)Data Access (ADO.NET)Directory AccessFile System, registry accessNetworking and web browsing.NET attributes and reflectionWinOS accessCOM interoperability12/3/2020.NET Framework Class LibraryThư viện nền tảng cho .NET FrameworkChia làm 2 phần: base class library và framework class library23NamespaceDescriptionSystemChứa lớp toán học, chuyển đổi dữ liệuSystem.IOCác lớp cho thao tác Input và OutputSystem.NetCác lớp liên quan đến network protocolSystem.CollectionsChức các lớp liên quan đến xử lý tập hợpSystem.DataCác lớp của ADO.NETSystem.DrawingCác lớp thực thi chức năng GUISystem.ThreadingCác lớp lập trình MultiThreadSystem.WebCác lớp liên quan đến HTTP protocolSystem.XmlCác lớp liên quan XMLSystem.Web.UISystem.Web.UI. WebControlsCung cấp lớp và interface để tạo ASP.NET Server Control và ASP.NET web pagesFCLBCL12/3/2020ASP.NET12/3/202024ASP.NETASP (Active Server Page)Server side scriptingCác file chứa HTML và scripting codeTruy cập thông qua HTTP Scripting code được interpreting ở server sideASP cho phépNhanh chóng và dễ dàng tạo ứng dụng WebKhởi tạo nội dung web độngValidation script ở phía clientSử dụng COM với chức năng mở rộng: truy cập DB12/3/202025Hạn chế của ASPTrộn lẫn giữa layout (HTML) và phần logic (script)Interpreting ASP dẫn đến hạn chế thực thiNgôn ngữ script không mạnh về kiểu dlJScript, VBScriptTương thích với trình duyệtQuản lý trạng thái kémCập nhật file khi server down12/3/202026ASP.NETNền tảng phát triển web mới của MSMô hình lập trình server –side mớiWeb ClientOperating SystemASP.NETApplicationsIIS.NET Framework12/3/202027ASP.NETTách bạch giữa layout và business logicSử dụng các dịch vụ được cung cấp bởi .NETCode được compile lần đầu tiên khi page được request Quản lý trạng tháiSử dụng nhiều ngôn ngữ lập trìnhCross language integrationCập nhật file trong khi server running!12/3/202028Class hierarchyNamespaceCấu trúc được phân cấpDot-syntax, những lớp được nhóm lại một cách logicNhiều lớp trừu tượng cơ sở và đa dạng các lớp thực thiCho phép lập trình viện thoải mái sử dụng & cũng như tạo riêng cho mình các lớpVí dụ: System.Web.UI.WebControls. ButtonNamespaceClass12/3/202029Class hierarchySystem.Web.UI. WebControlsListControlListBoxCheckBoxListButtonTableWebControlSystem.Web.UI.ControlSystem.ObjectTextBox......12/3/202030Đặc tính ASP.NETCú pháp ASP.NET và các ngôn ngữ hỗ trợTiến trình thực thiAssembliesQuản lý trạng thái, bảo mật và xử lý sự kiện12/3/202031Cú pháp ASP.NETLayout và Business logicKhông còn trộn lẫn giữa HTML và scripting codeDễ dàng maintainance cho Web appPhân biệt hoàn toàn giữa phần layout và phần xử lý logicKhông chứa code thực thi trong những file HTMLChia ra các file dành cho thiết kế & các file dành cho lập trìnhCó thể trộn lẫn code và layout nếu muốn!.aspx.cs.aspx12/3/202032Hỗ trợ đa ngôn ngữVisual BasicVBScript is unmanaged !JScriptC#New component-based languageC++Managed Extensions for C++Others: Cobol, Smalltalk, ...Common Language Specification (CLS)12/3/202033Các dạng file ASP.NETCác loại file phân biệt với tên phần mở rộngFile ASP.NET chuẩn: aspx, ascxWeb service: asmxCode (behind) file: .aspx.cs, .asmx.vbFile cấu hình: Web.configWeb applications: global.asax, global.asax.csTất cả file đều là dạng file textCách dễ dàng để bắt đầu từ file asp chuyển thành aspx12/3/202034Cú pháp ASP.NET pageDirectivesCode Declaration Blocks [ lines of code ] Code Render BlocksHTML Control Syntax 12/3/202035Cú pháp ASP.NET pageCustom Control SyntaxCustom server controlsServer control propertySubpropertyServer control event binding12/3/202036Cú pháp ASP.NET pageData Binding Expression’ runat=“server” />Server-side Object TagsServer-side Include DirectivesServer-side Comments12/3/202037ASP.NET sample void SubmitBtn_Click(object sender, EventArgs e) { Message.Text = "Hello " + Name.Text; } Name: 12/3/202038ASP.NET sample12/3/202039.aspx execution cycleCodebehindIISASP.NET RuntimeInstantiate controlsParse .aspx fileGenerate page classRequest .aspx fileResponseClientServer12/3/202040Execution processCompile khi trang được request lần đầu tiênMS intermediate language (MSIL)Dạng ngôn ngữ trừu tượngĐộc lập CPUMSIL được thực thi bởi CLRCommon Language RuntimeJust-in-Time compilerManaged code12/3/202041AssembliesKết quả của quá trình biên dịch là file *.exe/*.dllMột hoặc nhiều assembliesAssembly 1MyApp.dllpic1.jpgmetadata12/3/202042State ManagementApplication StateWhat is an “application”?Files, pages, modules, and executable codeOne virtual directory and its subdirectoriesApplication state variablesGlobal informationImplementation rulesUse of system resources“Lock” and “unlock” your global informationBeware of global variables in multithreaded environmentsLoss of state when host is “destroyed”No state sharing across Web farms12/3/202043State ManagementSession StateWhat is a session?Restricted to a logical applicationContext in which a user communicates with a serverFunctionalityRequest identification and classificationStore data across multiple requestsSession eventsRelease of session data.NET State Server Process12/3/202044SecurityReasons for SecurityPrevent access to areas of your Web serverRecord and store secure relevant user dataSecurity Configuration in Web.Config, , , ...Authentication, Authorization, ImpersonationCode Access SecurityAre you the code you told me you are?Protect your server from bad code12/3/202045SecurityAuthenticationValidates user credentialsAwards an authenticated identityTypes of authenticationWindows, integrating with IIS 5.0Passport, centralized services provided by MicrosoftForms, request attachmentAuthorizationDetermine whether request is permittedFile and URL authorization12/3/202046SecurityImpersonationIIS authenticates the “user”A token is passed to the ASP.NET applicationASP.NET impersonates the given tokenAccess is permitted according to NTFS settingsCode Access Security.NET Framework featureVerify the code‘s identity and where it comes fromSpecify operations the code is allowed to perform12/3/202047Event ModelApplication-level Event HandlingWeb FormsDelegate ModelConnect event sender and event receiverSingle and multicast delegatesEvent Delegates Are MulticastEvent WiringRegister event handler with event sender12/3/202048Event ModelEvents raised on client, but handled on serverServerWeb Clientparse messageeventevent handlereventmessageresponsecall appropriate event handler12/3/202049Why ASP.NET?Giải quyết được các hạn chế của ASPEnhanced performancePower and flexibilitySimplicityManageabilityScalability and availabilityCustomizability and extensibilityReliabilitySecurity12/3/202050Why ASP.NETMake it easier to build web appsMake it easier to deploy web appsMake it easier to operate web appsProvide greater performance, reliability, and scalability12/3/202051