A Base page in ASP.NET

后端 未结 5 1089
一向
一向 2021-02-08 06:04

Do you recommend from every web site created in Visual Studio, that you should create a Base page that serves as the parent class?

What are the exact benefits/drawbacks?

5条回答
  •  悲&欢浪女
    2021-02-08 06:27

    Yes, I do.

    But please remember that the purpose of a base page is totally different from the purpose of a master page.

    Let me explain.

    Master pages

    are layout elements used to share the same graphical features and part of the webforms behaviour (think a login/logout box with code-behind) across all pages that are associated to the master. Your final page classes will include a reference to the master page so the final result will appear as the master page including your page (check the source code to tell who contains whom)

    Base pages

    are (abstract? at least not sealed!) classes from which all your pages inherit from the code-behind view. Unless you explicitly and programmatically add controls to the basae page, ie. in the constructor via LoadControl method, all pages will look blank from the very beginning until you add code.

    But often they are useful. If you want to override some of the base class methods, you can have the overriden behaviour shared across all pages. Or, you may want to expose application-specific objects to the children pages (a reference to a data access layer, a logger or whatever). An example is overriding UICulture property to retrieve the user-preferred language from cookies.

    Both can be combined

    Depending on your goals, you may combine master pages with base pages.

    I suggest you to always create a base page class, since if your application's requirements change over time and you already created lots of pages, you can try to modify the base class to have the modifications propagated to all pages, according to the level of complexity of them.

提交回复
热议问题