Purpose: Use the. CS file in masterpage to replace pagebase in the project. Motivation: The motivation for writing this article comes from a project reconstruction. In. in the B/S architecture project of net framwork 2.0, the pagebase and masterpage technologies are used at the same time to find that accessing pagebase and masterpage at the same time not only reduces the performance, it may even cause a logical error to the function expansion and adjustment of the project in the future. Technical Links: Pagebase: A technology often used in. NET Framework 1.1 to encapsulate the same functions of multiple pages. Pagebase. CS class inherited from system. web. UI. page class, the project's web page inherits from pagebase. CS class. By Rewriting the page Initialization Method in the base class, you can call the business functions in pagebase, such as URL parameter verification and traffic storage (for specific implementation methods, see the official Microsoft example duwamishi ). Masterpage: a new feature in. NET Framework 2.0. It physically includes two files:. Master File (HTML Tag) and. CS file (C # code ).. The master file is used to draw the display layer, and the. CS file implements specific functions. A web page inherited from masterpage can inherit the display layer content in masterpage. Drawing a general page header and footer, and customizing a uniform layout, masterpage is a good choice. Simulation requirements: Use masterpage technology instead of pagebase to verify the address bar parameters. For a simple explanation, the login table information in the database is as follows: After logging on to the system, the URL address bar contains the following parameters: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1001 In this case, manually modify the parameters in the URL address bar as follows: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1002 The system will automatically jump back to the logon page. First code iteration: 1. Refer to the traditional pagebase method: The traditional PAGE method is as follows: Public class pagebase: system. Web. UI. Page { Public pagebase () { } /** // <Summary> /// Entry Method /// </Summary> Protected void initialize () { // Insert general business logic } } Web page: Public partial class testpage: pagebase { // The traditional method of calling pagebase /** // <Summary> /// Override the onpreinit () method of the base class and call the common Verification Method /// </Summary> /// <Param name = "E"> </param> Protected override void oninit (eventargs E) { Base. initialize (); } } Follow these steps to move the code in pagebase to masterpage: Masterpage. CS: Public partial class mymasterpage: system. Web. UI. masterpage { Protected void page_load (Object sender, eventargs E) { If (! Ispostback) { // Call the Verification Method Initialize (); } } } Modify the code on the Web page: Public partial class testpage: system. Web. UI. Page { // Call the method in master following the pagebase Method /** // <Summary> /// Override the onpreinit () method of the base class and call the common Verification Method /// </Summary> /// <Param name = "E"> </param> Protected override void oninit (eventargs E) { // Obtain the parent page reference Mymasterpage = (mymasterpage) This. master; // Call the common verification method on the motherboard page If (! Ispostback) { Mymasterpage. initialize (); } } } Replace the initialize () method in masterpage with the test code in the instance: Step 1: log on to the system with the user name zhangsan. The logon is successful, The page displays welcome to log on to zhangsan. URL address display: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1001 Step 2: manually modify the URL address bar as follows: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1002 The page does not show the welcome Lisi login, but jumps back to the login page. Reflection: although the functions are implemented, there are some unsatisfactory links: 1. the quilt class calling method in the master must be a public method; 2. Although you do not need to modify the inheritance of web pages, you still need to mechanically copy and paste the oninit () method of the base class. To eliminate these flavors, start: The second code iteration: Modify the code in masterpage. CS: Public partial class mymasterpage: system. Web. UI. masterpage { Protected void page_load (Object sender, eventargs E) { If (! Ispostback) { // Call the Verification Method Checklogin (); } } /** // <Summary> /// Verify that the access is valid /// </Summary> Private void checklogin () { // If the number in the URL or the number in the cookie If (string. isnullorempty (request. querystring ["ID"]) | String. isnullorempty (cookieutil. readcookiebykey ("ID "))) { Response. Redirect ("login. aspx "); } // If the number in the URL does not match the number in the cookie, return to the logon page Else if (Int. parse (request. querystring ["ID"])! = Int. parse (cookieutil. readcookiebykey ("ID "))) { Response. Redirect ("login. aspx "); } } } After reconstruction, the web page can be modified without any modification. masterpage automatically calls the verification method in its page_load () method, and sets the verification method to private, which is only called by masterpage, improve security. Now, the Code seems ideal. test: Step 1: log on to the system with the user name zhangsan, The User Logon page is still displayed. Test failed. Use breakpoint tracking code to find the code snippet in the checklogin () method in masterpage. CS: If (string. isnullorempty (request. querystring ["ID"]) | String. isnullorempty (cookieutil. readcookiebykey ("ID "))) { Response. Redirect ("login. aspx "); } Because the logon page inherits from masterpage, masterpage is automatically called when the page is loaded. the verification method in CS, and its own parameters do not meet the string. the isnullorempty () method jumps back to the login page, and the login page calls the verification method in the base class when loading again, forming an endless loop. In pagebase technology, web pages can be inherited from pagebase. In masterpage technology, in order to achieve consistent display layer effects, the selectivity of inheriting masterpage on Web pages is very low, in addition, we should not use the new masterpage with the same display and no verification code to inherit from the web pages that do not need to inherit the basic class functions. This method is obviously unreasonable. To solve this problem, we started The third iteration: Introduce the configuration file: <? XML version = "1.0" encoding = "UTF-8"?> <Pages> <Testpage> <Page title = "testpage" url = "testpage. aspx" needvalidate = "true"/> <Page title = "login" url = "login. aspx" needvalidate = "false"/> </Testpage> <Adminpages> <Page title = "page1" url = "~ /Admin/page1.aspx "needvalidate =" false "/> <Page title = "page2" url = "~ /Admin/page2.aspx "needvalidate =" false "/> </Adminpages> </Pages> You can see that the page to be verified is marked (needvalidate = "true "). Create an XML data sequence class: Public class xmldal { Private Static string filepath = string. empty; Static xmldal () { // Initialize the configuration file path Filepath = httpcontext. Current. Request. mappath ("~ /App_data/XML/"+" pages. xml "); } /** // <Summary> /// Obtain the list of pages to be verified /// </Summary> /// <Returns> List of pages to be verified </returns> Public static ilist <string> getvalidatepages () { Ilist <string> pages = new list <string> (); // If the specified configuration file exists If (system. Io. file. exists (filepath )) { Try { Xmldocument xmldoc = new xmldocument (); Xmldoc. Load (filepath ); // Obtain the root node of the configuration file Xmlnode root = xmldoc. documentelement; String XPath = "/pages/testpage/page [@ needvalidate = 'true']"; Xmlnodelist nodelist = root. selectnodes (XPath ); // Convenient Node Set Foreach (xmlnode node in nodelist) { Pages. Add (node. attributes ["title"]. value ); } } Catch (exception ex) { Throw new exception (ex. Message ); } } Return pages; } } Refactor the code in masterpage. CS and add the isvalidateneeded (string URL) method to check whether verification is required on the current page. Modify the verification method: Public partial class mymasterpage: system. Web. UI. masterpage { Protected void page_load (Object sender, eventargs E) { If (! Ispostback) { // Call the Verification Method Checklogin (); } } /** // <Summary> /// Verify that the access is valid /// </Summary> Private void checklogin () { // Determine whether verification is required for the current access page If (isvalidateneeded (request. rawurl )) { // If the number in the URL or the number in the cookie If (string. isnullorempty (request. querystring ["ID"]) | String. isnullorempty (cookieutil. readcookiebykey ("ID "))) { Response. Redirect ("login. aspx "); } // If the number in the URL does not match the number in the cookie, return to the logon page Else if (Int. parse (request. querystring ["ID"])! = Int. parse (cookieutil. readcookiebykey ("ID "))) { Response. Redirect ("login. aspx "); } } } /** // <Summary> /// Verify whether the current page needs verification /// </Summary> /// <Param name = "currentpage"> current page name </param> /// <Returns> whether to verify the status </returns> Private bool isvalidateneeded (string URL) { Bool isneeded = false; // The getvalidatepages () method returns the list of pages to be verified. Ilist <string> pages = xmldal. getvalidatepages (); Ienumerator <string> Ie = pages. getenumerator (); While (ie. movenext ()) { // If the current page requires verification If (URL. Contains (ie. Current )) // Return the status to be verified Return isneeded = true; } Return isneeded; } } Test: Step 1: log on to the system with the user name zhangsan. The logon is successful, The page displays welcome to log on to zhangsan. URL address display: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1001 Step 2: manually modify the URL address bar as follows: Http: // localhost: 3730/masterpagebasedemo/testpage. aspx? Id = 1002 The page does not show the welcome Lisi login, but jumps back to the login page. So far, my code iteration has ended. Download Code: Http://www.cnblogs.com/Files/ayuan/MasterPageBaseDemo.rar I have no experience in writing technical articles before, so the above texts will inevitably be obscure, and my technical skills are limited. Some ideas may not be very mature. Thank you for your correction. |