什麼是 Web FormWhat is Web Forms

ASP.NET Web Form 是 ASP.NET web 應用程式架構的一部分,並隨附Visual StudioASP.NET Web Forms is a part of the ASP.NET web application framework and is included with Visual Studio. 它是其中一個可用來建立 ASP.NET web 應用程式的四種程式設計模型,有些則是 ASP.NET MVC、 ASP.NET Web Pages 和 ASP.NET 單一頁面應用程式。It is one of the four programming models you can use to create ASP.NET web applications, the others are ASP.NET MVC, ASP.NET Web Pages, and ASP.NET Single Page Applications.

Web Form 是您的使用者要求使用瀏覽器的頁面。Web Forms are pages that your users request using their browser. 這些頁面可以使用 HTML 的組合來撰寫用戶端指令碼、 伺服器控制項和伺服器程式碼。These pages can be written using a combination of HTML, client-script, server controls, and server code. 當使用者要求的頁面時,進行編譯和程式架構,在伺服器上執行,然後架構會產生瀏覽器可以轉譯的 HTML 標記。When users request a page, it is compiled and executed on the server by the framework, and then the framework generates the HTML markup that the browser can render. ASP.NET Web Form 網頁將資訊呈現給使用者的任何瀏覽器或用戶端裝置。An ASP.NET Web Forms page presents information to the user in any browser or client device.

使用 Visual Studio,您可以建立 ASP.NET Web Form。Using Visual Studio, you can create ASP.NET Web Forms. Visual Studio 整合式開發環境 (IDE) 可讓您將拖放來配置您的 Web Form 網頁的伺服器控制項。The Visual Studio Integrated Development Environment (IDE) lets you drag and drop server controls to lay out your Web Forms page. 您可以輕鬆地將屬性、 方法和事件頁面上的控制項或頁面本身。You can then easily set properties, methods, and events for controls on the page or for the page itself. 這些屬性、 方法和事件用來定義網頁的行為、 外觀及操作、 等等。These properties, methods, and events are used to define the web page's behavior, look and feel, and so on. 若要撰寫伺服器程式碼來處理頁面的邏輯,您可以使用 Visual Basic 或 C# 等.NET 語言。To write server code to handle the logic for the page, you can use a .NET language like Visual Basic or C#.

Note

ASP.NET 和 Visual Studio 的文件橫跨數個版本。ASP.NET and Visual Studio documentation spans several versions. 反白顯示功能從舊版的主題可能適用於您目前的工作和使用最新版的案例。Topics that highlight features from previous versions may be useful for your current tasks and scenarios using the latest versions.

ASP.NET Web Form 是:ASP.NET Web Forms are:

  • 根據 Microsoft ASP.NET 技術,以動態方式在伺服器執行的程式碼會產生網頁輸出到瀏覽器或用戶端裝置。Based on Microsoft ASP.NET technology, in which code that runs on the server dynamically generates Web page output to the browser or client device.
  • 與任何瀏覽器或行動裝置的相容性。Compatible with any browser or mobile device. ASP.NET 網頁會自動轉譯功能,例如樣式、 配置和等等的正確符合瀏覽器的 HTML。An ASP.NET Web page automatically renders the correct browser-compliant HTML for features such as styles, layout, and so on.
  • 與任何支援.NET common language runtime,例如 Microsoft Visual Basic 和 Microsoft Visual C# 語言的相容性。Compatible with any language supported by the .NET common language runtime, such as Microsoft Visual Basic and Microsoft Visual C#.
  • Microsoft.NET Framework 上建置。Built on the Microsoft .NET Framework. 這會提供架構,包括受管理的環境、 型別安全和繼承的所有優點。This provides all the benefits of the framework, including a managed environment, type safety, and inheritance.
  • 具有彈性,因為您可以加入使用者建立和協力廠商的控制項。Flexible because you can add user-created and third party controls to them.

ASP.NET Web Form 的供應項目:ASP.NET Web Forms offer:

  • 從應用程式邏輯的 HTML 和其他 UI 程式碼分離。Separation of HTML and other UI code from application logic.
  • 一組豐富的常見工作,包括資料存取的伺服器控制項。A rich suite of server controls for common tasks, including data access.
  • 功能強大的資料繫結,很棒的工具支援。Powerful data binding, with great tool support.
  • 用戶端指令碼的瀏覽器中執行的支援。Support for client-side scripting that executes in the browser.
  • 支援的各種其他功能,包括路由、 安全性、 效能、 國際化、 測試、 偵錯、 錯誤處理和狀態管理。Support for a variety of other capabilities, including routing, security, performance, internationalization, testing, debugging, error handling and state management.

ASP.NET Web Forms 可協助您克服挑戰ASP.NET Web Forms Helps You Overcome Challenges

Web 應用程式開發的挑戰,通常不發生傳統的用戶端應用程式進行程式設計時。Web application programming presents challenges that do not typically arise when programming traditional client-based applications. 這些挑戰包括:Among the challenges are:

  • 實作豐富的 Web 使用者介面-可能很難和繁瑣來設計和實作使用者介面使用基本的 HTML 功能,特別是在頁面具有複雜的配置,大量的動態內容,且功能完整的使用者互動的物件。Implementing a rich Web user interface - It can be difficult and tedious to design and implement a user interface using basic HTML facilities, especially if the page has a complex layout, a large amount of dynamic content, and full-featured user-interactive objects.
  • 用戶端與伺服器分開-在 Web 應用程式、 用戶端 (瀏覽器) 和伺服器是不同的程式執行不同的電腦上 (而且甚至不同的作業系統)。Separation of client and server - In a Web application, the client (browser) and server are different programs often running on different computers (and even on different operating systems). 因此,在這兩半的應用程式共用的極少資訊;可以進行通訊,但通常交換只有小區塊的簡單的資訊。Consequently, the two halves of the application share very little information; they can communicate, but typically exchange only small chunks of simple information.
  • 無狀態執行-網頁伺服器收到的要求 頁面上,當它找到頁面、 加以處理,將它傳送至瀏覽器中,,然後會捨棄所有的頁面資訊。Stateless execution - When a Web server receives a request for a page, it finds the page, processes it, sends it to the browser, and then discards all page information. 如果使用者再次要求相同的頁面,伺服器就會重複整個序列,重新處理從零開始頁面。If the user requests the same page again, the server repeats the entire sequence, reprocessing the page from scratch. 換句話說,伺服器有沒有記憶體的它曾處理過的頁面,頁面都是無狀態。Put another way, a server has no memory of pages that it has processed—page are stateless. 因此,如果應用程式需要維護 頁面的相關資訊,其無狀態的本質可能會導致問題。Therefore, if an application needs to maintain information about a page, its stateless nature can become a problem.
  • 未知的用戶端功能-在許多情況下,Web 應用程式會使用不同的瀏覽器的許多使用者存取。Unknown client capabilities - In many cases, Web applications are accessible to many users using different browsers. 瀏覽器有不同的功能,因此很難建立將執行的應用程式上所有人都能正常。Browsers have different capabilities, making it difficult to create an application that will run equally well on all of them.
  • 資料存取的複雜度-讀取和寫入至傳統的 Web 應用程式中的資料來源,可以很複雜,而且需要大量資源。Complications with data access - Reading from and writing to a data source in traditional Web applications can be complicated and resource-intensive.
  • 擴充性很複雜-在許多情況下設計與現有方法的 Web 應用程式未達到延展性目標,因為缺乏應用程式的各種元件之間的相容性。Complications with scalability - In many cases Web applications designed with existing methods fail to meet scalability goals due to the lack of compatibility between the various components of the application. 這通常是大量成長循環中的應用程式的常見失敗點。This is often a common failure point for applications under a heavy growth cycle.

針對 Web 應用程式的這些挑戰,可能需要相當的時間和精力。Meeting these challenges for Web applications can require substantial time and effort. ASP.NET Web Form 和 ASP.NET framework 請透過下列方式解決這些挑戰:ASP.NET Web Forms and the ASP.NET framework address these challenges in the following ways:

  • 直覺式且一致的物件模型-ASP.NET 網頁架構提供的物件模型,可讓您將您的表單,做為一個單位,而非個別的用戶端和伺服器項目。Intuitive, consistent object model - The ASP.NET page framework presents an object model that enables you to think of your forms as a unit, not as separate client and server pieces. 在此模型中,您可以程式比傳統的 Web 應用程式,包括設定頁面元素的屬性和事件回應能力更直覺的方式。In this model, you can program the page in a more intuitive way than in traditional Web applications, including the ability to set properties for page elements and respond to events. 此外,ASP.NET 伺服器控制項是從 實體內容的 HTML 網頁和瀏覽器與伺服器之間的直接互動的抽象概念。In addition, ASP.NET server controls are an abstraction from the physical contents of an HTML page and from the direct interaction between browser and server. 一般情況下,您可以使用伺服器控制項的方式,您可能會在用戶端應用程式中使用控制項,而不需要思考如何建立 HTML 來呈現和處理控制項和其內容。In general, you can use server controls the way you might work with controls in a client application and not have to think about how to create the HTML to present and process the controls and their contents.
  • 事件導向的程式設計模型-ASP.NET Web Forms Web 應用程式中獲得熟悉的模型撰寫用戶端或伺服器上的事件處理常式發生的事件。Event-driven programming model - ASP.NET Web Forms bring to Web applications the familiar model of writing event handlers for events that occur on either the client or server. ASP.NET 網頁架構會抽象化此模型的基礎的機制,擷取用戶端上的事件、 將它傳輸到伺服器,以及呼叫適當的方法是全部自動化,且看不到您的方式。The ASP.NET page framework abstracts this model in such a way that the underlying mechanism of capturing an event on the client, transmitting it to the server, and calling the appropriate method is all automatic and invisible to you. 結果是支援事件驅動的開發清楚、 輕鬆地撰寫程式碼結構。The result is a clear, easily written code structure that supports event-driven development.
  • 直覺式的狀態管理-ASP.NET 網頁架構會自動處理維護您的頁面和它的控制項狀態的工作,並為您提供明確的方式維護狀態的應用程式特定資訊。Intuitive state management - The ASP.NET page framework automatically handles the task of maintaining the state of your page and its controls, and it provides you with explicit ways to maintain the state of application-specific information. 這不需要大量伺服器資源使用來完成,並可以實作,不論將 cookie 傳送至瀏覽器。This is accomplished without heavy use of server resources and can be implemented with or without sending cookies to the browser.
  • 獨立於瀏覽器的應用程式-ASP.NET 網頁架構可讓您建立在伺服器上,不需要明確撰寫程式碼的差異,在瀏覽器中的所有應用程式邏輯。Browser-independent applications - The ASP.NET page framework enables you to create all application logic on the server, eliminating the need to explicitly code for differences in browsers. 不過,它仍可讓您利用特定瀏覽器的功能撰寫的用戶端程式碼,以提供更佳的效能和更豐富的用戶端體驗。However, it still enables you to take advantage of browser-specific features by writing client-side code to provide improved performance and a richer client experience.
  • .NET framework common language runtime 支援-ASP.NET 網頁架構根據.NET Framework 中,因此整個架構是可供任何 ASP.NET 應用程式。.NET Framework common language runtime support - The ASP.NET page framework is built on the .NET Framework, so the entire framework is available to any ASP.NET application. 您的應用程式可以是任何語言所撰寫的是與執行階段的相容性。Your applications can be written in any language that is compatible that is with the runtime. 此外,資料存取是使用.NET Framework,包括 ADO.NET 所提供的資料存取基礎結構簡化。In addition, data access is simplified using the data access infrastructure provided by the .NET Framework, including ADO.NET.
  • .NET framework 可調整伺服器效能-ASP.NET 網頁架構可讓您調整 Web 應用程式從單一處理器的一部電腦至多部電腦的 Web 伺服陣列乾淨且不需要複雜的應用程式的變更邏輯。.NET Framework scalable server performance - The ASP.NET page framework enables you to scale your Web application from one computer with a single processor to a multi-computer Web farm cleanly and without complicated changes to the application's logic.

ASP.NET Web Form 的功能Features of ASP.NET Web Forms

  • 伺服器控制項-ASP.NET Web 伺服器控制項是要求頁面時執行的 ASP.NET Web 網頁和瀏覽器的呈現標記上的物件。Server Controls- ASP.NET Web server controls are objects on ASP.NET Web pages that run when the page is requested and that render markup to the browser. 許多 Web 伺服器控制項是類似於熟悉的 HTML 項目,例如按鈕和文字方塊。Many Web server controls are similar to familiar HTML elements, such as buttons and text boxes. 其他控制項包含複雜的行為,例如行事曆控制項,以及您可以使用以連接到資料來源,並顯示資料的控制項。Other controls encompass complex behavior, such as a calendar controls, and controls that you can use to connect to data sources and display data.
  • 主版頁面-ASP.NET 主版頁面允許您在您的應用程式建立一致的版面配置頁面。Master Pages- ASP.NET master pages allow you to create a consistent layout for the pages in your application. 單一的主版頁面定義應用程式中的外觀與風格及標準行為所需的所有頁面 (或頁面群組)。A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. 然後,您可以建立個別的內容頁面,其中包含您想要顯示的內容。You can then create individual content pages that contain the content you want to display. 當使用者要求內容的頁面時,它們會合併與主版頁面,以產生結合來自 [內容] 頁面的內容中的主版頁面的版面配置的輸出。When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.
  • 使用資料-ASP.NET 提供許多選項來儲存、 擷取及顯示資料。Working with Data- ASP.NET provides many options for storing, retrieving, and displaying data. 在 ASP.NET Web Forms 應用程式中,您可以使用資料繫結控制項來自動化的簡報或網頁 UI 項目,例如資料表和文字方塊和下拉式清單中的資料輸入。In an ASP.NET Web Forms application, you use data-bound controls to automate the presentation or input of data in web page UI elements such as tables and text boxes and drop-down lists.
  • 成員資格-ASP.NET Identity 儲存使用者的認證在應用程式所建立的資料庫。Membership- ASP.NET Identity stores your users' credentials in a database created by the application. 當您的使用者登入時,應用程式會驗證其認證,藉由讀取資料庫。When your users log in, the application validates their credentials by reading the database. 您的專案帳戶資料夾包含實作成員資格的各個部分的檔案: 註冊、 登入、 變更密碼,以及授權的存取。Your project's Account folder contains the files that implement the various parts of membership: registering, logging in, changing a password, and authorizing access. 此外,ASP.NET Web Form 支援 OAuth 和 OpenID。Additionally, ASP.NET Web Forms supports OAuth and OpenID. 這些驗證增強功能可讓使用者能夠登入您的網站使用這類帳戶,以及 Facebook、 Twitter、 Windows Live、 Google 中的現有認證。These authentication enhancements allow users to log into your site using existing credentials, from such accounts as Facebook, Twitter, Windows Live, and Google. 根據預設,此範本會建立使用 SQL Server Express LocalDB,隨附於 Visual Studio Express 2013 for Web 的開發資料庫伺服器執行個體上的預設資料庫名稱的成員資格資料庫。By default, the template creates a membership database using a default database name on an instance of SQL Server Express LocalDB, the development database server that comes with Visual Studio Express 2013 for Web.
  • 用戶端指令碼和用戶端架構-您可以包括在 ASP.NET Web Form 網頁中的用戶端指令碼的功能來增強 ASP.NET 伺服器功能。Client Script and Client Frameworks- You can enhance the server-based features of ASP.NET by including client-script functionality in ASP.NET Web Form pages. 您可以使用用戶端指令碼來為使用者提供更豐富、 更靈敏回應的使用者介面。You can use client script to provide a richer, more responsive user interface to users. 您也可以使用網頁瀏覽器中執行時,進行到 Web 伺服器的非同步呼叫的用戶端指令碼。You can also use client script to make asynchronous calls to the Web server while a page is running in the browser.
  • 路由-URL 路由可讓您設定應用程式以接受要求不會對應至實體檔案的 Url。Routing- URL routing allows you to configure an application to accept request URLs that do not map to physical files. 要求 URL 會是只要將使用者輸入他們的瀏覽器,在您的網站上尋找頁面的 URL。A request URL is simply the URL a user enters into their browser to find a page on your web site. 您使用路由來定義使用者具有語意意義,而且可以幫助搜尋引擎最佳化 (SEO) 的 Url。You use routing to define URLs that are semantically meaningful to users and that can help with search-engine optimization (SEO).
  • 狀態管理-ASP.NET Web Forms 可包含數個選項可協助您保留每個頁面為基礎和以整個應用程式的基礎上的資料。State Management- ASP.NET Web Forms includes several options that help you preserve data on both a per-page basis and an application-wide basis.
  • 安全性-開發更安全的應用程式的重要部分是了解它的威脅。Security- An important part of developing a more secure application is to understand the threats to it. Microsoft 開發了分類威脅的方式:詐騙、 竄改、 否認、 資訊洩漏、 阻絕服務,提高權限 (STRIDE)。Microsoft has developed a way to categorize threats: Spoofing, Tampering, Repudiation, Information disclosure, Denial of service, Elevation of privilege (STRIDE). 在 ASP.NET Web Forms 中,您可以新增擴充功能和組態選項,可讓您自訂 ASP.NET Web Form 中的各種安全性行為。In ASP.NET Web Forms, you can add extensibility points and configuration options that enable you to customize various security behaviors in ASP.NET Web Forms.
  • 效能-效能可能會成功的網站或專案中的關鍵因素。Performance- Performance can be a key factor in a successful Web site or project. ASP.NET Web Forms 可讓您修改效能相關網頁和伺服器控制項的處理、 狀態管理、 資料存取、 應用程式設定和載入,且有效率的編碼方式。ASP.NET Web Forms allows you to modify performance related to page and server control processing, state management, data access, application configuration and loading, and efficient coding practices.
  • 國際化-ASP.NET Web Forms 可讓您建立可以取得的網頁內容和其他資料會根據瀏覽器慣用的語言設定,或根據使用者的明確選擇的語言。Internationalization- ASP.NET Web Forms enables you to create web pages that can obtain content and other data based on the preferred language setting for the browser or based on the user's explicit choice of language. 內容和其他資料指資源,這類資料可以儲存在資源檔或其他來源。Content and other data is referred to as resources and such data can be stored in resource files or other sources. 在 ASP.NET Web Form 頁面中,您可以設定要從資源取得其屬性值的控制項。In an ASP.NET Web Forms page, you configure controls to get their property values from resources. 在執行階段,從適當的當地語系化的資源檔的資源取代資源運算式。At run time, the resource expressions are replaced by resources from the appropriate localized resource file.
  • 偵錯和錯誤處理-ASP.NET 包含功能,可協助您診斷您 Web Forms 應用程式中可能發生的問題。Debugging and Error Handling- ASP.NET includes features to help you diagnose problems that might arise in your Web Forms application. 偵錯和錯誤處理也支援在 ASP.NET Web Form,讓您的應用程式編譯,並有效地執行。Debugging and error handling are well supported within ASP.NET Web Forms so that your applications compile and run effectively.
  • 部署與裝載-Visual Studio、 ASP.NET、 Azure 以及 IIS 提供工具,協助您部署和裝載 Web Forms 應用程式的程序。Deployment and Hosting- Visual Studio, ASP.NET, Azure, and IIS provide tools that help you with the process of deploying and hosting your Web Forms application.

決定何時建立 Web Form 應用程式Deciding When to Create a Web Forms Application

您必須仔細考慮是否要實作的 Web 應用程式使用 ASP.NET Web Form 模型或另一個模型,例如 ASP.NET MVC 架構。You must consider carefully whether to implement a Web application by using either the ASP.NET Web Forms model or another model, such as the ASP.NET MVC framework. MVC 架構不會取代 Web Form 模型;您可以使用任一種架構的 Web 應用程式。The MVC framework does not replace the Web Forms model; you can use either framework for Web applications. 在決定要用於特定網站的 Web Form 模型或 MVC 架構之前,衡量每一種方法的優點。Before you decide to use the Web Forms model or the MVC framework for a specific Web site, weigh the advantages of each approach.

Web Form 為基礎的 Web 應用程式的優點Advantages of a Web Forms-Based Web Application

Web Form 為基礎的架構提供下列優點:The Web Forms-based framework offers the following advantages:

  • 它支援透過 HTTP,而這有益於開發業務的 Web 應用程式會保留狀態的事件模型。It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. Web Form 為基礎的應用程式提供許多的種支援數百種伺服器控制項的事件。The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.
  • 它會使用頁面控制器模式將功能加入至個別頁面。It uses a Page Controller pattern that adds functionality to individual pages. 如需詳細資訊,請參閱 < 頁面控制器MSDN 網站上。For more information, see Page Controller on the MSDN Web site.
  • 它會使用檢視狀態或伺服器為基礎的表單,可讓您更輕鬆的管理狀態資訊。It uses view state or server-based forms, which can make managing state information easier.
  • 它適用於 Web 開發人員和設計人員想要利用大量元件快速開發應用程式可用的小型團隊。It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.
  • 一般情況下,它是較不複雜的應用程式開發,因為元件 ( 頁面類別、 控制項等) 彼此緊密整合,通常需要比 MVC 模型少的程式碼。In general, it is less complex for application development, because the components (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.

MVC Web 應用程式的優點Advantages of an MVC-Based Web Application

ASP.NET MVC 架構提供下列優點:The ASP.NET MVC framework offers the following advantages:

  • 它可讓您更輕鬆地將應用程式分割成模型、 檢視和控制器來管理複雜性。It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.
  • 它不使用檢視狀態或伺服器表單。It does not use view state or server-based forms. 這使得 MVC framework 非常適合開發人員想要充分控制應用程式的行為。This makes the MVC framework ideal for developers who want full control over the behavior of an application.
  • 它會使用前端控制器模式,以處理 Web 應用程式要求,透過單一控制站。It uses a Front Controller pattern that processes Web application requests through a single controller. 這可讓您設計支援豐富的路由基礎結構的應用程式。This enables you to design an application that supports a rich routing infrastructure. 如需詳細資訊,請參閱 < 前端控制器MSDN 網站上。For more information, see Front Controller on the MSDN Web site.
  • 它提供更佳的支援測試導向開發 (TDD)。It provides better support for test-driven development (TDD).
  • 它非常適合大型團隊的開發人員和 Web 設計人員需要較高程度的控制應用程式的行為所支援的 Web 應用程式。It works well for Web applications that are supported by large teams of developers and Web designers who need a high degree of control over the application behavior.