Beginning ASP.NET MVC 4英文清晰版

所需积分/C币:10 2014-02-28 10:00:52 4.57MB PDF
收藏 收藏
举报

对英语要求不高,我的CET4的水平读下来除了词汇感觉不够,语法理解起来不是问题(有些语句的理解值得推敲)。 我是之前学过MVC3 也写过蹩脚的程序 但是一直没深入的学习过 类 方法 思想。只是快速通过baidu 整出来了 不求甚解 !但是会发现捉襟见肘的情况 就是有些情况 无法解决 基础不牢 就是这样! 理科书籍 无需过多的描写 首先集成老外教科书的优良传统 不是随便的拼凑 作为beginning 作者思路清晰 按照工......
About the author About the technical reviewer Acknowledgments mmmmmannmmaaaaammamaamnmmamaamamnammmmaann. Xvii ntroduction XIX Chapter 1: Introducing ASPNeT Mvc 4amaaaaar ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Chapter 2: Installing ASPNET Mvc 4 Chapter 3: The"Have You Seen Me? " ASPNET MVC 4 Web Application 25 Chapter 4: Controllers ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 39 Chapter5:VeWS,,,,,,,,,,,,,,,,,,,,,,,,,,47 Chapter 6: Models ■■■■■■■I 69 Chapter 7: Data Validation mRmm aBBIaIaIm 95 Chapter 8: Ajax and jquerymem ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■口 111 Chapter 9: Security mRRRRRRRIB RB ERR RRRRIIT 139 Chapter10: Routing,围围围围围围187 Chapter 11: Testing the Application ua197 Chapter 12: Deploying the Application a ■■■■■■■■■■■■■■■■■■■■■■■■■■■ u227 Index 275 With the introduction of ASP.NET MVC in 2009, Microsoft offered developers a new approach to building web applications based on the Microsoft stack. By combining the power and maturity of ASP.NET and the. NET Framework with the advantages of the mvc pattern, AsP. NET MVC offers web application developers important features, such as testability, full control over the generated HTML, and great support for large teams working on the same project by separating the application into the model, the view, and the controller The purpose of this book is to introduce you to the latest version of ASP. NET MVC, version 4. To ground the book in the real world of developing web applications the concepts presented in all the chapters are examined in the context of a sample web application introduction to ASP. NET and discusses where MVC fits into the whole ASPNET framework. Chapter 2 describes ef The book is basically divided into three parts. The first part, comprising Chapters 1 through 3, starts with a bri the software requirements and the different options for installing ASP.NET MVC 4. Chapter 3 explains the sample application you are going to build and the tools that you need to build it, and then steps you through the actual creation and configuration of the sample application The second part, Chapters 4 through 6, explains the three core components of the mvc pattern Chapter 4 examines controllers, including how controllers handle user requests through action methods and how results are produced. Controllers is the first concept as that's where it all beings, handling a request from the user. Chapter 5 explains views and how HTML is generated. It shows how to process server-side code with the new Razor view engine and how to produce appropriate output for desktop and mobile devices. Generating a response to the user is the second concept as is normally what happens after the request is processed. Finally, Chapter 6 examines models It defines the domain model of the application by showing how to create the data model using Entity Framework and the business models using c# classes. It also examines what view models are and how to use them effectively to produce the final hTmL in the browser. The third part Chapters 7-12, examines different aspects of the asp. net mvc framework, such as data validation (Chapter 7), security(Chapter 9), and routing( Chapter 10). Chapter 8 shows you how to work with client-side code using jQuery and ajax, and Chapter 1l demonstrates how to test the different aspects of your application using unit testing. Finally, Chapter 12 explains how to deploy the application to servers you control and to Windows azure I hope that this book gives you a great foundation upon which to start building web applications using ASPNET MVC 4 We will be using Visual Studio 2012 for the examples in the book. You can use any of the paid versions (Professional, Premium or Ultimate)or the fee Express version. Also, some of the images you will see are based on modifications made to Visual Studio in update 2, so you will need to install that update as well. You can download VisualStudio2012andupdate2fromhttp://bit.ly/vs2012DownLoad CHAPTER 1 When Microsoft first introduced ASP.NET in 2002, it had a single programming model called Web Forms that allowed developers to quickly build web applications in a way that was familiar (to those who built applications with visual Studio) and intuitive. At the time, the Web Forms model was a breakthrough. although in many ways the model was a black box, it definitely moved the state of web development forward In Web Forms, web pages are (normally represented by two files, one with extension. aspx that defines the user interface, and another with extension aspx cs or aspx vb( depending on the server language, Ct or VB. NeT)that is used to write code to implement event handlers(e.g button clicks, list-item selection, etc and other functionality Also, the applications usually have a page called Default. aspx that represents the home page for the website The inherent problems that this black box produced (poor separation of concerns, extremely difficult automated testing, minimal control on server controls rendered HTML, etc. have been criticized over the years, and that opened the door to new development patterns and technologies. While Mvc is not a new concept, it brought a fresh breath of life to AsP. NET along with capabilities that were extremely difficult, if not impossible, in Web Forms This chapter introduces AsP. NET in general, the Web Forms programming model, the asP. NET Web Pages model, and ASP. NET MVC itself, including the MvC pattern and the ASP.NET MvC 4 history, benefits, features, and architecture What is aspnet There are three technologies for building web applications in ASP. NET: ASP. NET Web Forms(the original programming model) ASP.NET Web Pages ASPNET MVC We'll look at each of these three technologies in turn in the following sections, but first let's discuss the asP. NEt olatform-that is, the substrate technology upon which all ASP.NET applications run AccordingtoMicrosoftsdocumentation(http://bit.ly/aspnetOveRviEw) ASP. NET is a Web platform that provides all the services that you require to build enterprise-class server-based Web applicationS. ASP. NET is built on the. NET Framework, so all. NET framework features are available to ASP.NET applications. Your applications can be written in any language that is compatible with the common language runtime (CLr, including visual Basic and C# To create ASP. NET Web applications, you can use Visual Studio. The tools and options in Visual Studio that are designed for creating Web applications are referred to collectively as Visual Web Developer. In addition, a free standalone product-Visual Web Developer Express-is available that includes the core set of Web-design features from visual studio CHAPTER 1 INTRODUCING ASPNET MVC 4 ASP.NET includes the following features A page and controls framework The asP.neT compiler Security infrastructure State-management facilities Application configuration Health monitoring and performance features Debugging support An XML web services framework, which was later superseded by Windows Communication Foundation ( WCF) An extensible hosting environment and application lifecycle management An extensible designer environment So, we have a rich and powerful environment in which we can build our web applications by using the language of our choice-and it's free. In this environment, we can build applications even with Notepad, but a more sophisticated tool would increase our productivity by doing a lot of work for us; yes I'm talking about microsoft visual Studio. There are paid versions of Visual Studio, but if money is a problem, then you can use the free Visual Web Developer exi One of the major benefits of ASP.NET is the change from interpreted code, previously used for Classic ASP (the programming model before ASP. NET), to compiled code, allowing web applications to have better performance Common Intermediate Language(CIL), which is commonly referred as Microsoft Intermediate Language(MsIl) tes When our code in the application is first compiled by the high-level language(c#, VB. NET, etc compiler, it generates code(an assembly language supercharged with lots of vitamins and minerals). The Msil code is later taken by the NET runtime to generate native machine code. Web applications created with ASP. NET are executed by the. NET Framework, not the operating system This makes our applications type-safe and has the advantage of automatic memory garbage collection. Additionally, the. NET Framework provides structured error handling and multithreading support. Finally, information about classes, members, and all of our code in general is stored as metadata in the assemblies generated at compile time. To deploy asP.NEt applications, you can use one of the different techniques available, such as Web Deploy or, the simplest method, a file copy to the server. Deploying asp.nEt applications is a fairly simple process considering that normally the. NET Framework is already installed on the server (and if not, it can be bundled with our applications) After our applications are in the server, we need to setup microsoft s web server, Internet Information Services (Iis), which will host all ASP. NET applications and serve the applications to end users It is important to note that ASP. NET is fully object-oriented(OO), meaning that not only the code we write but also the code supporting AsP NET is oo. Your code will have full access to all objects in the. NET Framework, and you can also implement all the goodies of an OO environment(inheritance, polymorphism, and encapsulation) ASP.NET and the. NET Framework have come a long way since ASP.NET'S first release, version 1.0, and the minor update release 1.I. Version 2.0, released in 2005, added richer functionality with new controls, master pages, themes and skins, web parts, full pre-compilation, and many more features. A year later, version 3.0 added Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), Windows Workflow Foundation (WF) and windows CardSpace In. nET 3. 5 Microsoft added even more controls and asP. NET AJAX was already built into the framework, WCF added support for RSS, jSoN, POX and partial trust. NET 3. 5 Service Pack l introduced Dynamic Data along with improvements for AJAX, JavaScript combining and new namespaces. Version 4.0, released in 2010, added a new set of features, such as extensible output caching, jQuery as the default Java Script library, routing in the framework, a much better ViewState control, and a lot of improvements to existing functionality. Finally, the latest version 4.5, released in August 2012, includes asynchronous operations on Http requests responses modules and handlers, strongly typed data controls, model binding, unobtrusive validation, and HTML5, among other features CHAPTER 1 INTRODUCING ASPNET MVC 4 ASPNET Web forms ASP.NET Web Forms allows you to create web applications in the same way you would create a traditional windows Forms application. This means that you have a control-based interface and the following two files for each page A user interface(UD) file: Includes the markup and is where you design how your page will look and which controls it will use. This file has the extension. aspx A code-behind file: Includes the code that handles events and interactions of all the obj in the page( this code could be included on the preceding. aspx page, but normally is in a separate file). This file has an extension associated with the programming language, either aspx cs for C# or aspx. vb for VB. Net Whenever asp. net processes a page, the page passes through several stages, each of which raises different events to handle the processing of the page and its controls You write code to handle these events and thus respond to various actions related to the processing of a page. For example, you might write code that gets called when the user licks a button. When a page is first requested, you often have to initialize data and controls. However, when the page posts back, you dont need to run this code A postback happens when a control on the page raises an event that must be handled by the server view state is the information about the page controls status. After each postback, the page view state is modified with the new statuses of all the controls in the page. As a default, the page view state is stored in a hidden field inside each page (see Figure 1-1), and its scope and lifetime are limited to the page it belongs to <input type=hidden" name=" EvENTTARGET" id=" EVENTTARGET" value=""/ EⅤ NTARGUMENT EVENTARGUMENT input type=mnen"name=网sTT"1=VIE可s工耳 TE va1e="/wPD订区T3MTE0De1Mg9kEg工c9xEgc39 KFgJmDxO Figure 1-1. The view state's hidden field The main use of view state is to preserve form information across postbacks. View state is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a postback This behavior can be modified, however, as view state can be disabled on a per-control, per-page, or server-wide basis Also, as a security measure, no sensitive information should be stored in view state because the serialized string can be easily deserialized are processed. Table 1-1 lists these events and the effect they have on the page and its control events and how they To work effectively with Web Forms, it is very important to understand the page life-cycle Table 1-1. Page Life-cycle events Reinit This is the first real event you might handle for a page. You typically use this event only if you need to dynamically(from code) set values such as master page or theme This event is also useful when you are working with dynamically created controls for a page. You want to create the controls inside this event Init This event fires after each control has been initialized you can use this event to change initialization values for controls InitComplete This event is raised after all initializations of the page and its controls have been completed Preload This event fires before view state has been loaded for the page and its controls and before postback processing (continued) CHAPTER 1 INTRODUCING ASPNET MVC 4 Table 1-1.(continued) Load The page is stable at this time; it has been initialized and its state has been reconstructed Code inside the page load event typically checks for PostBack and then sets control properties appropriatel The page' s load event is called first followed by the load event for all the controls on the web page. Control(PostBack) ASP. NET now calls any events on the page or its controls that caused the postback to occur. event(s) This might be a buttons click event, for example LoadComplete This event occurs after all postback data and view state data is loaded into the page and after the Load method has been called for all controls on the page PreRender Use this event to perform any updates before the server controls are rendered to the page. Since all server controls know how to render themselves, this event fires just before that so you can modify any default behavior Render This is a method of the page object and its controls(and not an event). At this point, ASP.NET calls this method on each of the page's controls. The Render method generates the client-side HTML, Dynamic HTML(DHTML), and script that are necessary to properly display a control at the browser This method is useful if you are writing your own custom control. You can override this method to influence the html generated for the control This event is used for cleanup code You use it to release any managed resources in this stage. Managed resources are resources that are handled by the runtime, such as instances of classes created by the. NET CLR An AsP.NEt web application contains several types of files, and each type serves a specific purpose within the application. Table 1-2 lists and describes the most important files in an ASP. NET web application Table 1-2. ASP. NET File Types aspX This ASP. NET Web Forms file contains the markup (or Ul)of the web page and, optionally the underlying application code aspx cs or aspx vb These are the code-behind files csorv These are files for general code classes web confi This is the applications general configuration file. It is an XML file that contains all settings for customizing the connection strings, application settings, security, external assemblies, memory, state management, and so on bal asax In this file, you can add code for event handlers at the application level. Events are those for when the application starts or ends or when an error is thrown. You can also define Session State events for when a session starts or ends These are user control files In these files, you can create small pieces of ui real state (e g a reusable address panel) the same way as with a full aspx page, but the difference is that they cannot be accessed directly and must be hosted inside. aspx pages. You can reuse these user controls in any page of your applications (continued CHAPTER 1 INTRODUCING ASPNET MVC 4 Table 1-2.(continued) asmxor. svc ASMX files are AsP. NET web services, introduce in. NET 2. 0. These files provide services for pages in the application or any other program that can access them. ASMX web services are now being replaced by Windows Communication Foundation (WCF)services(introduced in. NET 3.0), which have the extension. svc and offer much-improved security and scalability features master Master pages are like ASPX pages but are used as templates for other pages, having the look and feel and base functionality We can create two types of asp. net web applications in Visual Studio ● aSP net web sites ASP.NET web applications Web sites are preferable in certain scenarios, including the following You want to include both C# and visual Basic code in a single web project. By default, a web application is compiled based on language settings in the project file. Exceptions can be made but making an exception is relatively difficult You want to open the production site in Visual Studio and update it in real time by using FtP. You do not want to have to explicitly compile the project in order to deploy it. If you do precompile the site, you want the compiler to create multiple assemblies for the site, which can include one assembly per page or user control, or one or more assemblies per folder. You want to be able to update individual files in production by just copying new versions to the production server, or by editing the files directly on the production server If you precompile the site, you want to be able to update individual asP. nEt web pages (ASPX files )without having to recompile the entire site You like to keep your source code on the production server because it can serve as an additional backup copy Web applications, on the other hand, offer better functionality if You want to be able to use the edit and continue feature of the visual Studio debugger You want to run unit tests on code that is in the class files that are associated with aspnet pages. You want to refer to the classes that are associated with pages and user controls from stand-alone classes You want to establish project dependencies between multiple web projects You want the compiler to create a single assembly for the entire site You want control over the assembly name and version number generated for the site You want to use MSBuild or Team Foundation Build to compile the project. For example, you might want to add prebuild and postbuild steps You want to avoid putting source code on a production server. CHAPTER 1 INTRODUCING ASPNET MVC 4 Another important choice to make is the type of controls we'll have in our application. We can choose HTML or web server controls, or both. In general, a Web Forms web page can contain basic controls such as type="text". />, which are standard to HTML, and/or much more powerful web server controls such as <asp: TextBox.../>. The difference between the two types of controls is functionality. HTML controls have limited functionality because they work only on the client (browser), while web server controls work on both the client and the server. Web server controls are the only ones that are accessible in the code-behind file of the web page, and they generate the appropriate hTMl markup when rendered to the client Consider using html controls when any of the following conditions exists You are migrating existing, classic AsP pages over to aSP. NET. The control needs to have custom client-side JavaScript attached to the controls events The web page has lots of client-side JavaScript that is referencing the control In nearly all other cases you should consider using the more powerful web server controls which follow a programming model and naming standard similar to Windows Forms. a web server control can generate extremely complex htMl markup. For example, <asp: Calendar> renders a<table> with multiple <tr>and <td>elements These controls also have other benefits, such as multi browser rendering support, a powerful programming model, layout control, and theme support Note For more information about the differences between html server controls and web server controls visit http://bit.ly/aspnetwEbsErvErcOntrOls ASP NET Web Pages ASP.NET Web Pages(now in version 2)is a framework for building web applications in which pages use the Ra azor svntax ASP. NET Razor uses a simple programming syntax that lets you embed server-based code into a web page. Razor pages use the extension. cShtml or vbhtml depending on the language of choice(C# or VB. NET). Because the code is embedded in the web page, the idea of a code-behind file doesn't exist in Razor. The content in Razor pages is created with HTML, and there are no web server controls such as <asp button 7> You can use Visual Studio to create asP. net Web Pages but the main tool to use is microsoft Web Matrix 2 With WebMatrix 2, you have access to web helpers, which let your applications use common services such as Twitter, reCAPTCHA, Gravatars, Bing Maps, and more. Also, your applications can be extended using packages from the NuGet Gallery. You use NuGet's graphic interface or command line window to request the packages you want and then they will be downloaded, installed, and configured in your application automatically WebMatrix 2 also includes SQL Server Compact Edition(SQL CE), a lightweight, free, embedded database solution. You can create edit, and delete database schema and data directly within WebMatrix. You can run web applications in WebMatrix using IIS Express, which is included when you install it; that way, you dont have to worry about configuring a full-blown iiS just to test your application Support for Windows Azure is also baked into WebMatrix 2. You can publish your application directly to Windows Azure, but what's really nice is that you can open your application directly from Windows Azure and make changes to it NoteFormoreinformationaboutAsp.neTWebPagesvisithttp://bit.ly/aspnetwEbpAgeS

...展开详情
试读 127P Beginning ASP.NET MVC 4英文清晰版
立即下载 低至0.43元/次 身份认证VIP会员低至7折
    抢沙发
    一个资源只可评论一次,评论内容不能少于5个字
    关注 私信 TA的资源
    上传资源赚积分,得勋章
    最新推荐
    Beginning ASP.NET MVC 4英文清晰版 10积分/C币 立即下载
    1/127
    Beginning ASP.NET MVC 4英文清晰版第1页
    Beginning ASP.NET MVC 4英文清晰版第2页
    Beginning ASP.NET MVC 4英文清晰版第3页
    Beginning ASP.NET MVC 4英文清晰版第4页
    Beginning ASP.NET MVC 4英文清晰版第5页
    Beginning ASP.NET MVC 4英文清晰版第6页
    Beginning ASP.NET MVC 4英文清晰版第7页
    Beginning ASP.NET MVC 4英文清晰版第8页
    Beginning ASP.NET MVC 4英文清晰版第9页
    Beginning ASP.NET MVC 4英文清晰版第10页
    Beginning ASP.NET MVC 4英文清晰版第11页
    Beginning ASP.NET MVC 4英文清晰版第12页
    Beginning ASP.NET MVC 4英文清晰版第13页
    Beginning ASP.NET MVC 4英文清晰版第14页
    Beginning ASP.NET MVC 4英文清晰版第15页
    Beginning ASP.NET MVC 4英文清晰版第16页
    Beginning ASP.NET MVC 4英文清晰版第17页
    Beginning ASP.NET MVC 4英文清晰版第18页
    Beginning ASP.NET MVC 4英文清晰版第19页
    Beginning ASP.NET MVC 4英文清晰版第20页

    试读已结束,剩余107页未读...

    10积分/C币 立即下载 >