首页
会员中心
到顶部
到尾部
外文翻译

ASP.NET入门C#版(外文翻译及原文)

时间:2020/10/15 9:18:37  作者:  来源:  查看:0  评论:0
内容摘要: ASP.NET入门C#版ASP(动态服务器主页)是一种较新的技术,它已经过几个阶段的发展(进化).它是怎么诞生的呢?在七年前,它作为一种简单的方法来往普通网页里添加动态内容。自从那时以后,它的发展势头强劲:作为高级网页程序的开发平台,包括:电子商务网站、基于事件驱动的门户...
ASP.NET入门C#
ASP(动态服务器主页)是一种较新的技术,它已经过几个阶段的发展(进化).它是怎么诞生的呢?在七年前,它作为一种简单的方法来往普通网页里添加动态内容。自从那时以后,它的发展势头强劲:作为高级网页程序的开发平台,包括:电子商务网站、基于事件驱动的门户网站和你在网上能看到的其他所有东西。ASP.NET 2.0 ASP的最新版本,而且拥有最让人激动的更新。在ASP.NET帮助下,在网络编程时,开发者不再把一大堆HTML源码和脚本代码杂乱地放在同一页面上。你可以完全使用代码和工具(Visual Studio 2005)来创建网页程序。这些创新的代价仅仅是多学一些东西。你要学一些高级开发工具(Visual Studio)和工具包(the .NET Framework),而且你需要精通一门编程语言,如C#。网络发展的演化因特网是在1960年末在试验中诞生的。它的目标是:建立一个真实的、有弹性的信息网络——可以经受的起若干数量的电脑的崩溃,而不至于阻断其他电脑的正常通信。经得起潜在的重大灾难(如核武器攻击)。美国国防部提供了刚开始的研究基金。最早的因特网局限在教育机构和从事国防的单位。它因为作为学术研究的一种工具而繁荣,它让全球的研究人员可以彼此共享信息。到了1990初,伟大的“猫”诞生了,它通过电话线工作,从此,因特网向商业用户打开了大门。在1993年,第一个HTML浏览器诞生了,标志着因特网革命的到来。我们很难把最早的网页称为网页序。第一代的网页看起来更像小册子:主要由固定的HTML页面构成,这些也都需要手动修改。 一个简单的HTML页面有点像一个字处理文档——它包含了格式化的内容,可以在你的电脑上显示,但是并不完成其他任何功能。上面的就是一个最简单的例子,文档包括头信息和单行文本。一个HTML文档有两种类型的内容:文本和标记(告诉浏览器如何格式化)。这些标记很容易辨认,因为它们总是出现在<  >之间。HTML定义了不同级别的标题、段落、超链接、斜体和粗体格式、水平线等。举个例子:<h1>某个文本<h1>,告诉浏览器用标题1的格式来显示这个文本,就是用最大的黑体字来显示。图11显示了这个页面在浏览器中的效果。提示:你不需要精通HTML就能进行ASP.NET网页编程,虽然它(HTML)是很有用的。为了快速介绍一下HTML,给大家介绍一个网上的优秀HTML指南。HTML 2.0 首次引入了一个网页编程的新技术,称为HTML表单。HTML表单扩展了HTML的功能,不仅包含了格式化标签,而且包含了窗体小部件或者叫控件。这些控件包含了普通的功能部件,如下拉列表、文本框和按钮。下面是一个由HTML表单控件创建的网页。网页表单允许网页程序设计师设计标准的输入页面。当用户单击图12的提交按钮,所有在输入控件中的数据(在这个例子中是两个复选框)将打包成一个长字符串,接着发送到服务器。在服务器端,一个客户程序接收和处理这些数据。令人惊奇的是:这些为HTML表单创建有超过十年之久的控件仍然是你用来创建ASP.NET页面的基础。不同的是这些程序控件将运行在服务器端。在过去,当用户单击一个表单页面的按钮时,信息要通过e-mail来发送或者使用在服务器端运行的程序(通过CGI标准)。今天,你将可以使用更强大、更优雅的ASP.NET平台。理解ASP.NET的创建原因可以帮助我们了解其他网页开发技术遇到的问题。在原始的CGI标准下,举个例子,网页服务器必须为每个网页请求建立一个单独的程序实例。如果这个网页很受人们欢迎(那访问者将很多),那么网页服务器就要经受得起数以百计的独立程序副本,这样最终导致服务器反而因为受欢迎而成为受害者。为了解决这个问题,微软开发了ISAPI(网络服务程序编程接口),一个高层次的编程模型。ISAPI解决了性能问题,但是付出了复杂性的代价。即使ISAPI开发者是个C+编程老手,他仍然晚上担心到失眠,因为会遇到多线程处理这样让人麻烦的问题。ISAPI编程是给那些坚强的“夜猫子”,不是给那些懦弱的人。ISAPI并没有真正消失,取代它的是,微软利用它建立了一个更高级的开发平台,如ASP  ASP.NET。这两种技术都可以使开发者编写动态网页,而不需要担心底层的执行细节。由于这个原因,这两个平台成功到令人难以置信。最初的ASP平台吸引了将近一百万的开发人员。当ASP.NET第一次发布时,作为.NET Framework的核心部件受到人们更多关注。事实上,ASP.NET 1.0已经在数十个大型商业网络中得到应用,虽然它还在最后的测试阶段。虽然拥有类似的基础,ASP ASP.NET有根本的不同。ASP是基于脚本的编程语言,需要全面理解HTML,而且还要经过一大堆痛苦的编程训练。而ASP.NET,在另一方面,是面向对象的编程模型,建立网页页面就像建立桌面程序一样容易。在很多方面,学会ASP.NET比精通ASP要容易,而且ASP.NET功能更加强大。同时,服务器端的网络发展为从技术的字母形花片汤到广受欢迎的一类编程开发人员开始试着使用嵌入多媒体、JavaScript的小程序、DHTMLJava代码来增强网页的功能。这些基于客户端的技术不需要通过服务器的处理就能实现。所有的程序都(从服务器)下载到客户端浏览器,在本地执行。客户端技术的最大问题就是它们不被所有的浏览器和操作系统完美的支持。其中的一个原因就是网络开发太受欢迎了,首先是网络程序不需要通过CD 装、下载和其他单调的配置。取而代之的是,一个网络程序只要能上网的电脑就可以执行了。但是,一旦开发者使用客户端技术,他们就会遇到一些常见的问题:比 如跨浏览器的兼容性。开发者就不得不在不同的浏览器和操作系统中测试他们的网页,甚至他们还要给用户发布浏览器更新。换句话说,客户端模型牺牲了网络程序 最重要的优良特性。由于上述原因,ASP.NET被设计为服务器端技术。所有的ASP.NET代码都在服务器上执行。当代码执行完毕时,用户就会得到一个普通的HTML页面,这样任何浏览器都可以浏览了。图13显示了服务器端和客户端模型的不同。这里还有几条原因要避免客户端编程:孤立性:客户端代码无法访问服务器资源。举个例子,没有一种简单的方式让客户端可以读取一个在服务器上的文件或进行数据库连接。(至少不会遇到安全性和浏览器兼容性的问题) 安全性:最终用户可以查看客户端代码。一旦有不怀好意的用户理解了程序是怎么工作的,他们就有可能乱来了。在某些方面,ASP.NET允许你通过服务器端编程结合最佳的客户端编程。举个例子:ASP.NET控件可以智能侦测客户端浏览器的属性。如果该浏览器支持JavaScript,那么这些控件将返回一个含有JavaScript的更多功能的页面。尽管如此,不管浏览器的功能有多强大,你的代码始终在服务器端执行。状态限制:为了保证最佳性能,网络设计成无状态的协议。意思就是:一旦页面已经传送给用户,连接就关闭了而且用户指定的信息也被丢弃了。ASP包括一个session state(会话状态)特性允许程序员来解决这个问题。使用session state,一个网页程序可以为每一个客户端暂时保存信息(保存在服务器的内存里)。尽管如此,如果一个网站是放在几个服务器上,session state就无能无力了。在这种情况下,一个客户要访问B服务器,而他的会话信息是被A服务器保留的,这样实际上这个会话信息将被丢弃掉。ASP.NET纠正了这个问题,允许把状态储存在中央仓库,就像一个单独的进程或者一个所有服务器都可以访问的数据库。ASP.NET通过引进全新的模型解决了上述问题(当然不止这些啦)。这个模型是基于一个伟大的技术,称之为.NET Framework。你应该知道的是:.NET Framework是几种技术的群集(集合)。.NET语言:包括CVB.NET(Visual Basic .NET一种面向对象的、现代化的语言(VB 6.0的继任者);这些语言还包括:Jscirpt.NET(服务器端版本的JavaScriptJ#(java的兼容产品),还有C++管理扩充。CLR(公共语言运行库):CLR是执行所有.NET程序和为这些程序提供自动服务的引擎,如安全验证、内存管理和优化等。.NET Framework类库:类库包含了成千上万个已经预建好的函数,你可以在你的程序中嵌入它们。这些众多属性有时也被成为一个技术集,如ADO.NET(用来创建数据库程序的技术)和Windows Forms(也是一种技术,用来创建基于桌面的用户界面程序)。ASP.NET:这是一种主机网页程序和网络服务的引擎,从.NET类库中包含了几乎所有特性。ASP.NET还包含了网页特有的服务。Visual Studio:这个可选的开发工具包含了众多提高效率和调试功能的特性。VS的安装CD(或DVD)包含了完整的.NET Framework,所以你不需要额外下载它。有趣的是,C#和VB.NETC#和Java要相似多了(或者是VB6VB.NET)。虽然语法是不同的,但是C#和VB.NET都使用.NET类库,也都由CLR支持。事实上,几乎所有的C#代码块都可以一行一行翻译成VB.NET的代码块。当然也有不行的时候(如:C#语言支持一种属性叫调用匿名方法,而VB.NET不支持)。但是对绝大部分来说,只要开发者学会了其中一个.NET语言,就可以很快学会另一种。简而言之,C#和VB.NET都是一流的,现代的用来开发下一代网络程序的语言。.NET 1.0引进了一种全新的语言。尽管如此,.NET 2.0语言的变化还是细微的。C# 2005VB2005都添加了一些新的特性,但是这些语言绝大部分都没有变化。因此,任何使用C1.0编写的代码都可以同样的在2.0下运行。在第二、三章,就就会学到C#语法和面向对象编程的基础。搞定了这些基础,你就可以开始创建简单的网页了。这样你就会少点困惑,学得更快。(向一些高级话题进军,如数据库访问和网络服务)CLR(公共语言运行库)只能运行IL代码,这就意味着它根本不知道你的源代码是用哪一个语言编写的,尽管如此,CLR竟然完成了另外一个编译步骤——它接受了IL代码并把它转换成适合当前平台的本机机器语言。这个步骤在程序启动时发生,而且在代码被真正执行之前。在ASP.NET程序中,当网络程序正在运行时,这些特定机器文件存储在高速缓存中,所以它们可以被复用(重新使用),以确保最佳性能。你或许会问.NET为什么不直接编译成机器语言。原因是:机器代码取决于多个因素,包括CPU。举个例子,如果你是为一台含有Intel 处理器的电脑而创建的机器代码,那么编译器将能够使用超线程技术来增强你的代码。这种适用特点机器的版本并不适合在其他电脑中运行,因为你无法保证它们使用同样的处理器。在VS 2005中,另一个受人欢迎的改变是支持不同的编码模型。而VS2003却受那个困扰,VS 2005支持某个范围的不同编码模型,使它成为具有灵活性、通用性我的设计工具。这就让你可以把HTML标签和事件处理代码放在同一个文件类或者分开存放,而不用委屈的使用VS,这样可以享受有用的特性好处,如代码智能完成。
Beginning ASP.NET 2.0 in C#
ASP (Active Server Pages) is a relatively new technology that’s already leapt through several stages of evolution. It was introduced about seven years ago as an easy way to add dynamic content to ordinary web pages. Since then, it’s grown into something much more ambitious: a platform for creating advanced web applications, including e-commerce shops, data-driven portal sites, and just about anything else you can find on the Internet.ASP.NET 2.0 is the latest version of ASP, and it represents the most dramatic change yet. With ASP.NET, developers no longer need to paste together a jumble of HTML and script code in order to program the Web. Instead, you can create full-scale web applications using nothing but code and a design tool such as Visual Studio 2005. The cost of all this innovation is the learning curve. Not only do you need to learn how to use an advanced design tool (Visual Studio) and a toolkit of objects (the .NET Framework), you also need to master a programming language such as C#.The Internet began in the late 1960s as an experiment. Its goal was to create a truly resilient information network—one that could withstand the loss of several computers without preventing the others from communicating. Driven by potential disaster scenar-ios (such as nuclear attack), the U.S. Department of Defense provided the initial funding.The early Internet was mostly limited to educational institutions and defense contrac-tors. It flourished as a tool for academic collaboration, allowing researchers across the globe to share information. In the early 1990s, modems were created that could work over existing phone lines, and the Internet began to open up to commercial users. In 1993, the first HTML browser was created, and the Internet revolution began..It would be difficult to describe early websites as web applications. Instead, the first gen-eration of websites often looked more like brochures, consisting mostly of fixed HTML pages that needed to be updated by hand.basic HTML page is a little like a word-processing document—it contains formatted content that can be displayed on your computer, but it doesn’t actually do anything. The following example shows HTML at its simplest, with a document that contains a heading and single line of text:An HTML document has two types of content: the text and the tags that tell the browser how to format it. The tags are easily recognizable, because they occur inside angled brack-ets (< >). HTML defines tags for different levels of headings, paragraphs, hyperlinks, italic and bold formatting, horizontal lines, and so on. For example, <h1>Some Text</h1> tells the browser to display Some Text in the Heading 1 style, which uses a large, bold font. Figure 1-1 shows the simple HTML page in a browser.HTML 2.0 introduced the first seed of web programming with a technology called HTML forms. HTML forms expand HTML so that it includes not only formatting tags but also tags for graphical widgets, or controls. These controls include common ingredients such as drop-down lists, text boxes, and buttons. Here’s a sample web page created with HTML form controls:HTML forms allow web application developers to design standard input pages. When the user clicks the Submit button on the page shown in Figure 1-2, all the data in the input controls (in this case, the two check boxes) is patched together into one long string and sent to the web server. On the server side, a custom application receives and processes the data. Amazingly enough, the controls that were created for HTML forms more than ten years ago are still the basic foundation that you’ll use to build dynamic ASP.NET pages! The difference is the type of application that runs on the server side. In the past, when the user clicked a button on a form page, the information might have been e-mailed to a set account or sent to an application on the server that used the challenging CGI (Common Gateway Interface) standard. Today, you’ll work with the much more capable and elegant ASP.NET platform.To understand why ASP.NET was created, it helps to understand the problems of otherweb development technologies. With the original CGI standard, for example, the web server must launch a completely separate instance of the application for each web request. If the website is popular, the web server must struggle under the weight of hundreds of separate copies of the application, eventually becoming a victim of its own success.o counter this problem, Microsoft developed ISAPI (Internet Server Application Programming Interface), a higher-level programming model. ISAPI solved the perfor-mance problem but at the cost of significant complexity. Even after ISAPI developers master the tricky C++ programming language, they still lie awake at night worrying about confounding issues such as multithreading. ISAPI programming is definitely not for the fainthearted.ISAPI never really went away. Instead, Microsoft used it to build higher-level develop-ment platforms, such as ASP and ASP.NET. Both of these technologies allow developers to program dynamic web pages without worrying about the low-level implementation details. For that reason, both platforms have become incredibly successful. The original ASP platform garnered a huge audience of nearly one million developers. When ASP.NET was first released, it generated even more interest as the centerpiece of the .NET Frame-work. In fact, ASP.NET 1.0 was enthusiastically put to work in dozens of large-scale commercial websites even when it was only in late beta.Despite having similar underpinnings, ASP and ASP.NET are radically different. ASP is a script-based programming language that requires a thorough understanding of HTML and a good deal of painful coding. ASP.NET, on the other hand, is an object-oriented pro-gramming model that lets you put together a web page as easily as you would build a Windows application. In many respects, it’s easier to learn ASP.NET than to master ASP, even though ASP.NET is far more powerful.At the same time that server-side web development was moving through an alphabet soup of technologies, a new type of programming was gaining popularity. Developers began to experiment with the different ways they could enhance web pages by embed-ding multimedia and miniature applets built with JavaScript, DHTML (Dynamic HTML), and Java code. These client-side technologies don’t involve any server processing. Instead, the complete application is downloaded to the client browser, which executes it locally.The greatest problem with client-side technologies is that they aren’t supported equally by all browsers and operating systems. One of the reasons that web development is so popular in the first place is because web applications don’t require setup CDs, down-loads, and other tedious (and error-prone) deployment steps. Instead, a web application can be used on any computer that has Internet access. But when developers use client-side technologies, they encounter a few familiar headaches. Suddenly, cross-browser compatibility becomes a problem. Developers are forced to test their websites with differ-ent operating systems and browsers, and they might even need to distribute browser updates to their clients. In other words, the client-side model sacrifices some of the most important benefits of web development.or that reason, ASP.NET is designed as a server-side technology. All ASP.NET code executes on the server. When the code is finished executing, the user receives an ordinary HTML page, which can be viewed in any browser. Figure 1-3 shows the difference between the server-side and client-side model.These are some other reasons for avoiding client-side programming:Isolation: Client-side code can’t access server-side resources. For example, a client-side application has no easy way to read a file or interact with a database on the server (at least not without running into problems with security and browser compatibility).Security: End users can view client-side code. And once malicious users understand how an application works, they can often tamper with it.ome cases, ASP.NET allows you to combine the best of client-side programming with server-side programming. For example, the best ASP.NET controls can intelligently detect the features of the client browser. If the browser supports JavaScript, these controls will return a web page that incorporates JavaScript for a richer, more responsive user interface. However, no matter what the capabilities of the browser, your code is always executed on the server.State limitations: To ensure optimum performance, the Web is built on stateless protocols, which means as soon as a page is sent to a user, the connection is closed and any user-specific information is discarded. ASP includes a session state feature that allows programmers to work around this problem. Using session state, a web application can retain temporary information about each client in server memory. However, session state is useless in scenarios where a website is hosted by several separate web servers. In this scenario, a client might access server B while its session information is trapped on server A and essentially abandoned. ASP.NET corrects this problem by allowing state to be stored in a central repository, such as a separate process or a database that all servers can access.ASP.NET deals with these problems (and many more) by introducing a completely new model for web pages. This model is based on a remarkable piece of technology called the NET Framework.You should understand that the .NET Framework is really a cluster of several technologies:The .NET languages: These include C# and VB .NET (Visual Basic .NET), the object-oriented and modernized successor to Visual Basic 6.0; these languages also include JScript .NET (a server-side version of JavaScript), J# (a Java clone), and C++ with Managed Extensions.he CLR (Common Language Runtime): The CLR is the engine that executes all .NET programs and provides automatic services for these applications, such as security checking, memory management, and optimization.The .NET Framework class library: The class library collects thousands of pieces of prebuilt functionality that you can “snap in” to your applications. These features are sometimes organized into technology sets, such as ADO.NET (the technology for creating database applications) and Windows Forms (the technology for creating desktop user interfaces).ASP.NET: This is the engine that hosts web applications and web services, with almost any feature from the .NET class library. ASP.NET also includes a set of web-specific services.sual Studio: This optional development tool contains a rich set of productivity and debugging features. The Visual Studio setup CDs (or DVD) include the complete .NET Framework, so you won’t need to download it separately.Interestingly, C# and VB .NET are actually far more similar than Java and C# or than VB6 and VB .NET. Though the syntax is different, both C# and VB .NET use the .NET classlibrary and are supported by the CLR. In fact, almost any block of C# code can be translated, line by line, into an equivalent block of VB .NET code. An occasional language difference pops up (for example, C# supports a language feature called anonymous methods, while VB .NET doesn’t), but for the most part, a developer who has learned one .NETlanguage can move quickly and efficiently to another.n short, both C# and VB .NET are elegant, modern languages that are ideal for creating the next generation of web applications..NET 1.0 introduced completely new languages. However, the changes in the .NET 2.0 languages are much more subtle. Both C# 2005 and VB 2005 add a few new features, but most parts of these languages remain unchanged. As a result, any code written according to version 1.0 of the C# language will work identically with version 2.0. In Chapters 2 and 3, you’ll sort through the syntax of C# and learn the basics of object-oriented programming. By learning the fundamentals before you start creating simple web pages, you’ll face less confusion and move more rapidly to advanced topics such as database access and web services.he CLR runs only IL code, which means it has no idea which .NET language you orig-inally used. Notice, however, that the CLR actually performs another compilation step—it takes the IL code and transforms it to native machine language code that’s appropriate for the current platform. This step occurs when the application is launched, just before the code is actually executed. In an ASP.NET application, these machine-specific files are cached while the web application is running so that they can be reused, ensuring optimum performance.You might wonder why .NET compilers don’t compile straight to machine code. The reason is that the machine code depends on several factors, including the CPU. For example, if you create machine code for a computer with an Intel processor, the compiler may be able to use Hyper-Threading to produce enhanced code. This machine-specific version isn’t suitable for deployment to other computers, because no guarantee exists that they’re using the same processor.Another welcome change in Visual Studio 2005 is the support for different coding models. While Visual Studio .NET 2003 locked developers into one approach, Visual Studio 2005 supports a range of different coding models, making it a flexible, all-purpose design tool. That means you can choose to put your HTML tags and event handling code in the same file or in separate files without compromising your ability to use Visual Studio and benefit from helpful features such as IntelliSense.
 
  


相关评论
广告联系QQ:45157718 点击这里给我发消息 电话:13516821613 杭州余杭东港路118号雷恩国际科技创新园  网站技术支持:黄菊华互联网工作室 浙ICP备06056032号