• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMasterPageDrawbacks母版页的缺点

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

You are wondering whether master pages are a good solution in ASP.NET. The author thinks master pages are a needlessly confusing and slow solution to a problem that confounds object-oriented programming and inheritance. Here we look at why the author thinks master pages are not an ideal solution in many cases.

Avoiding ASP.NET master pages(避免使用母版页)

We will start with one of the biggest master page problems: the performance problems. Having a master page will result in another assembly in your project in many settings. This is an extra burden, and in the author's experience removing master pages has resulted in better performance.

Confusion(容易混淆)

Master pages are confusing because they do not inherit as you might think they should. For a content page to access object data based on the master page, it must take a confusing route and use Master.Page.FindControls(). Here's some code that shows how to change the value of a TextBox when using a master page.

// How content page can change TextBox.
TextBox mpTextBox = mpContentPlaceHolder.FindControl("TextBox1") as TextBox;
if (mpTextBox != null)
{
    mpTextBox.Text = "TextBox found!";
}

Alternative without master pages. Why do we need to use FindControl here? Because the content page is a sibling, not a child of the master page. It cannot access the control directly. Here's how you could do it in the same page.

// How regular page can change TextBox.
TextBox1.Text = "TextBox found";

IntelliSense(智能感知)

Perhaps the biggest advantage of Visual Studio is that it has a powerful programming "helper" that tells you whether something exists and what it is. In the second example above, IntelliSense will work and tell you that the TextBox exists. In content pages, it will not.

Cache directives(缓存指令)

You want to have a site-wide caching scheme, but master pages will not let you specify it there. You must change the cache settings of each content page separately. If you have few pages, this is fine, but it becomes less practical as your site grows.

<%-- Can't put on master page. --%>
<%@ OutputCache Duration="600" VaryByParam="file" %>

Site growth(网站增长)

A good use of a content page that uses a master page is to have one for the "contact us" or "about us" page. However, if your business grows a lot, you might need ten contact pages. You then have 10 more source files, and many more files to compile.

Content versus code. This is the most severe drawback of master pages. If a website has any significant data, it must enforce a clear separation between data and code. Content pages allow embedded code in data. The result is a site that works but is needlessly slow and complicated. It's better to consolidate your logic together, and separate your data.

Data separation. Master pages do work better with fewer content pages. However, the whole point of content pages is to have content. They also always allow code, which confounds the programming model of content/code separation. You could more easily avoid the master page, and use logic to change parts of the page.

Summary(小结)

Here we looked at some drawbacks of master pages in the ASP.NET web framework. The author's opinion is that it is better to eliminate master pages for less confusion, better performance, and a clearer separation of code and data. This will result in fewer files to compile and fewer assemblies with easier deployment. You site can adhere to the object-oriented inheritance model instead.

http://dotnetperls.com/master-page-drawbacks


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转】CKEditor3.0.1在asp.net下进行配置的方法加图片上传发布时间:2022-07-10
下一篇:
asp.net控件使用时的问题发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap