建立ASP。网络应用-最佳实践

时间:2022-12-07 04:06:46

We are building an ASP.NET application and would like to follow the best practices. Some of the best practices are:

我们正在构建一个ASP。NET应用,并希望遵循最佳实践。一些最佳做法是:


Server side Code:

服务器端代码:

  • Use catch blocks to trap & log low level errors too.
  • 使用catch块来捕获和记录低级错误。
  • Use Cache objects to populate dropdowns etc. where we won’t expect the changes in the underlying data/database.
  • 使用缓存对象填充下拉列表等等,我们不希望在底层数据/数据库中发生更改。
  • In case of error logging framework, provide emailing alerts along with logging the errors.
  • 如果是错误日志框架,请在记录错误的同时提供邮件提醒。

HTML code: - Don’t write inline CSS. - Place the JavaScript code (If needed by the page) at the end of the page unless the page needs it for load time actions.

HTML代码:-不要写内联CSS。-将JavaScript代码(如果页面需要的话)放在页面的末尾,除非页面在加载时需要它。


Now coming to the point, Would you please share these best practice points if you have a comprehensive list of them already?

现在到了重点,如果你已经有了一个完整的清单,你愿意分享这些最佳实践点吗?

7 个解决方案

#1


25  

Some of the best practices that I've learned over time and written up for use at my company...many are mainly applicable to WebForms and not MVC.

我学到的一些最好的实践,并在我的公司中使用。许多主要适用于WebForms,而不是MVC。

  • Don't write .NET code directly in your ASPX markup (unless it is for databinding, i.e. Evals). If you have a code behind, this puts code for a page in more than one place and makes the code less manageable. Put all .NET code in your code-behind.
  • 不要直接在ASPX标记中编写. net代码(除非是用于数据绑定的,比如Evals)。如果您有一个代码,这就会在多个地方放置一个页面的代码,并使代码更容易管理。将所有的。net代码放在代码后。
  • SessionPageStatePersister can be used in conjunction with ViewState to make ViewState useful without increasing page sizes. Overriding the Page's PageStatePersister with a new SessionPageStatePersister will store all ViewState data in memory, and will only store an encrypted key on the client side.
  • SessionPageStatePersister可以与ViewState一起使用,使ViewState在不增加页面大小的情况下变得有用。用一个新的SessionPageStatePersister重写页面的PageStatePersister,它将在内存中存储所有的ViewState数据,并且只在客户端存储一个加密的密钥。
  • Create a BasePage that your pages can inherit from in order to reuse common code between pages. Create a MasterPage for your pages for visual inheritance. Pages with vastly different visual styles should use a different MasterPage.
  • 创建页面可以继承的BasePage,以便在页面之间重用公共代码。为页面创建一个主页以实现视觉继承。具有不同视觉风格的页面应该使用不同的主页。
  • Create an enum of page parameter key names on each WebForm that are passed in via the URL, to setup strongly-typed page parameters. This prevents the need for hard-coded page parameter key strings and their probable mis-typing, as well as allowing strongly-typed parameter access from other pages.
  • 在通过URL传入的每个web表单上创建一个页面参数键名的枚举,以设置强类型的页面参数。这避免了需要硬编码的页面参数键字符串及其可能的错误输入,以及允许从其他页面访问强类型的参数。
  • Make use of the ASP.NET Cache in order to cache frequently used information from your database. Build (or reuse from another project) a generic caching layer that will wrap the ASP.NET Cache.
  • 利用ASP。NET缓存,以便从数据库缓存经常使用的信息。构建(或从另一个项目重用)将包装ASP的通用缓存层。网络缓存。
  • Wrap ViewState objects with Properties on your Pages to avoid development mistakes in spelling, etc. when referencing items from the ViewState collection.
  • 在引用ViewState集合中的项时,将ViewState对象包装为页面上的属性,以避免在拼写等方面出现开发错误。
  • Avoid putting large objects and object graphs in ViewState, use it mainly for storing IDs or very simple DTO objects.
  • 避免将大型对象和对象图放在ViewState中,主要用于存储id或非常简单的DTO对象。
  • Wrap the ASP.NET Session with a SessionManager to avoid development mistakes in spelling, etc. when referencing items from Session.
  • 包装的ASP。与SessionManager的NET会话,以避免在引用会话中的项时在拼写等方面出现开发错误。
  • Make extensive use of the applicationSettings key/value configuration values in the web.config - wrap the Configuration.ApplicationSettings with a class that can be used to easily retrieve configuration settings without having to remember the keys from the web.config.
  • 在web上广泛使用应用程序设置键/值配置值。配置——包装配置。ApplicationSettings包含一个类,可以用来轻松检索配置设置,而不需要记住web.config中的键。
  • Avoid the easiness of setting display properties on your UI controls, instead use CSS styles and classes - this will make your styles more manageable.
  • 避免在UI控件上设置显示属性,而是使用CSS样式和类——这将使您的样式更易于管理。
  • Create UserControls in your application in order to reuse common UI functionality throughout your pages. For example, if a drop down list containing a collection of categories will be used in many places in the site - create a CategoryPicker control that will data bind itself when the page is loaded.
  • 在应用程序中创建用户控件,以便在整个页面中重用公共UI功能。例如,如果包含类别集合的下拉列表将在站点的许多地方使用——创建一个CategoryPicker控件,该控件将在页面加载时绑定自己的数据。
  • Use Properties on your UserControls to setup things like default values, different displays between pages, etc. Value type properties can be defined on your UserControls and then be set in your ASP.NET markup by using class level properties on UserControls.
  • 在用户控件上使用属性设置默认值、页面之间的不同显示等等。值类型属性可以在用户控件上定义,然后在ASP中设置。在UserControls中使用类级属性的净标记。
  • Make use of the ASP.NET validation controls to perform simple validations, or use the CustomValidator to perform complex validations.
  • 利用ASP。NET验证控件执行简单的验证,或者使用CustomValidator执行复杂的验证。
  • Create an Error handling page that can be redirected to when an unhandled exception occurs within your website. The redirection can occur via the Page_Error event in your Page, the Application_Error event in your Global.asax, or within the section within the web.config.
  • 创建一个错误处理页面,当网站中出现未处理的异常时可以重定向到该页面。重定向可以通过页面中的Page_Error事件、全局中的Application_Error事件发生。asax,或web.config中的部分。
  • When working with pages that use a highly dynamic data driven display, use the 3rd party (free) DynamicControlsPlaceholder control to simplify the code needed to save the state of dynamically added controls between postbacks.
  • 使用高度动态数据驱动显示的页面时,使用第三方(免费)DynamicControlsPlaceholder控件来简化保存回发之间动态添加控件状态所需的代码。

#2


19  

  1. Create a base page for all your asp.net pages. This page will derive from System.Web.UI.Page and you may put this in YourApp.Web.UI. Let all your asp.net pages dervice from YourApp.Web.UI.Page class. This can reduce lot of pain.

    为所有asp.net页面创建一个基础页面。这个页面将派生自System.Web.UI。你可以把这个放到你的app。web。ui中。让你的asp.net页面从你的app . web . ui中删除。页面类。这可以减少很多痛苦。

  2. Use Application_OnError handler to gracefully handle any error or exception. You should log the critical exception and send the details of the exception along with date-time and IP of client to the admin email id. Yes ELMAH is sure way to go.

    使用Application_OnError处理程序优雅地处理任何错误或异常。您应该记录关键异常,并将异常的详细信息连同客户的日期时间和IP发送到管理电子邮件id。

  3. Use ASP.NET Themes. Many developers don't use it. Do use them - they are a great deal.

    使用ASP。净的主题。许多开发人员不使用它。一定要使用它们——它们非常有用。

  4. Use MembershipProvider and RoleProvider. And Never use inbuilt ProfileProvider - They store everything in plain strings. It will drastically slow-down the performance while performing R/W

    使用MembershipProvider和RoleProvider类。不要使用内置的ProfileProvider——它们将所有内容存储在纯字符串中。它将在执行R/W时显著降低性能

  5. Use Firebug for client-side debugging. Try to follow YSlow standards for web-applications. Use YSlow extension for FireBug.

    使用Firebug进行客户端调试。尝试遵循web应用程序的YSlow标准。对FireBug使用YSlow扩展。

  6. Use jQuery for client-scripting.

    对客户端脚本使用jQuery。

  7. Never store User Authentication information in session or don't use sessions to judge if user is logged on. Store only minimum necessary information in sessions.

    不要在会话中存储用户身份验证信息,也不要使用会话来判断用户是否登录。在会话中只存储必要的最小信息。

  8. Have a look at PostSharp. Can improve maintainability of your code and make you more productive.

    看一下PostSharp。可以提高代码的可维护性,使您的工作效率更高。

  9. Never ever Deploy asp.net application under debug configuration on production. Find out here what scottgu has to say about this.

    永远不要在生产的调试配置下部署asp.net应用程序。看看斯科特古是怎么说的。

  10. User Web Deployment projects. It can transform web.config sections and replace with production server setings. It will merge all compiled code-behind classes into one single assembly which is a great deal.

    用户Web部署项目。它可以变换网络。配置部分并替换为生产服务器的设置。它将把所有编译后的代码隐藏类合并到一个单独的程序集中,这是非常重要的。

  11. Use Cookie-less domains to serve static resources like images, scripts, styles etc. Each client request is sent along with whole bunch of cookies, you don't need cookies while serving pictures or scripts. So host those resources on a cookie-less domain.

    使用无cookie的域来服务静态资源,如图像、脚本、样式等。每个客户端请求都随一系列cookie一起发送,在提供图片或脚本时不需要cookie。因此,将这些资源托管在一个无cookie的域中。

  12. Minify scripts, stylesheets and HTML response from the server. Removing unnecessary line-breaks and white-spaces can improve the time-to-load and bandwidth optimization.

    缩小脚本、样式表和来自服务器的HTML响应。消除不必要的断行和空白可以改进加载时间和带宽优化。

#3


7  

Forms:

形式:

  1. Set Page.Form.DefaultFocus and Page.Form.DefaultButton to improve user experience

    设置Page.Form。DefaultFocus Page.Form。DefaultButton以改进用户体验

  2. Check Page.IsValid in your Save button handler before proceeding.

    检查页面。在继续之前,保存按钮处理程序是有效的。

General:

一般:

  1. Understand and implement the techniques found in the article "TRULY Understanding ViewState"

    理解并实现文章中发现的“真正理解ViewState”的技术

  2. Use Page.IsPostBack in your page events to stop code from running unnecessarily.

    使用页面。IsPostBack用于阻止不必要的代码运行。

  3. Use hyperlinks instead of posting and using Response.Redirect whenever possible.

    使用超链接而不是发布和使用响应。重定向。

    a. Understand and use the second parameter of Response.Redirect (it "Indicates whether execution of the current page should terminate")

    a.理解并使用响应的第二个参数。重定向(它“指示当前页的执行是否应该终止”)

  4. Use the Page Lifecycle properly.

    正确使用页面生命周期。

  5. Use the Per-Request cache (HttpContext.Items) instead of Cache where it makes sense.

    使用每个请求缓存(HttpContext.Items),而不是在有意义的地方缓存。

Web.Config:

. config:

  1. Deploy with <compilation debug="false">

    与 <编译部署调试= " false>

  2. Register your controls at the web.config level instead of the page level (i.e. @Register).

    在web上注册控件。配置级别而不是页面级别(例如@Register)。

Themes:

主题:

  1. When using Themes, put your static images in the Theme as well.

    当使用主题时,将静态图像放在主题中。

    a. Don't link to the images directly from your markup, link to them from a skin file or css file in your Theme instead.

    a.不要直接从标记链接到图像,而是从主题中的皮肤文件或css文件链接到它们。

    ex: <asp:Image SkinID="MyImage" runat="server" ImageUrl="Images/myImage.gif" />

    例:

#4


4  

I don't think try/catch blocks are always appropriate for low-level methods. You want to catch (and log/alert, even better!) any errors before they get to the user, of course. But it is often better for a low-level method to just lets its errors get raised up to a higher level. The problem I have seen with low-level error trapping is that it often lets a larger operation continue, but then a result that is not quite correct gets presented to the user or saved to the database, and in the long run it's much more difficult to fix. It's also just a lot of extra clutter in your code to put a try/catch at every level if you're not going to "do anything" with the error until it's raised up to a higher level.

我认为try/catch块并不总是适合低级方法。当然,您希望在错误到达用户之前捕获(以及日志/警报)任何错误。但是对于一个低级的方法来说,最好是让它的错误被提升到更高的级别。我所见过的低级错误捕获的问题是,它通常会让更大的操作继续,但随后不太正确的结果会呈现给用户或保存到数据库中,从长远来看,修复起来要困难得多。如果您不打算对错误“做任何事情”,直到错误被提升到更高的级别,那么在您的代码中添加每一层的try/catch也会造成很多额外的混乱。

#5


2  

Here are some similar questions that may help you.

这里有一些类似的问题可以帮助你。

.NET best practices?

net的最佳实践?

Best way to learn .NET/OOP best practices?

学习.NET/OOP最佳实践的最佳方式?

This should probably be community wiki as well.

这应该也是社区wiki。

#6


1  

I would recommend a couple of books if you are interested in pursuing a journey to become a better, more productive developer. These books are language agnostic and as you can see by the user reviews, very very helpful.

如果你想成为一个更好、更高效的开发人员,我建议你读几本书。这些书是语言不可知论的,你可以从用户评论中看到,非常有用。

Code Complete 2

代码完成2

Pragmatic Programmer

务实程序员

If you are looking for a .NET specific book, you may appreciate the following book:

如果你正在寻找一本。net特定的书,你可能会喜欢以下的书:

Microsoft Application Architecture Guide [available online for free outside of print format]

微软应用程序架构指南[可在网上免费获取打印格式以外的内容]

#7


1  

ASP.NET

ASP.NET

  • If you don't use Session state, don't forget to turn off it.
  • 如果不使用会话状态,不要忘记关闭它。
  • Use Server.Transfer instead of Response.Redirect if possible.
  • 使用服务器。转移,而不是回应。如果可能的话,重定向。
  • Set expiration parameter in IIS.
  • 在IIS中设置过期参数。
  • Use GZip to compress the text files.
  • 使用GZip压缩文本文件。
  • Use Server-Side and Client-Side validation together.
  • 同时使用服务器端和客户端验证。
  • Use Url Rewriter or Routing to make friendly url for SEO.
  • 使用Url重写器或路由为SEO创建友好的Url。

Design

设计

  • Write each class and its properties of your CSS file in the same line. (To decrease the file size)
  • 在同一行中编写CSS文件的每个类及其属性。(减少档案大小)
  • Use CSS Sprites. (To decrease request)
  • 使用CSS精灵。(减少请求)

#1


25  

Some of the best practices that I've learned over time and written up for use at my company...many are mainly applicable to WebForms and not MVC.

我学到的一些最好的实践,并在我的公司中使用。许多主要适用于WebForms,而不是MVC。

  • Don't write .NET code directly in your ASPX markup (unless it is for databinding, i.e. Evals). If you have a code behind, this puts code for a page in more than one place and makes the code less manageable. Put all .NET code in your code-behind.
  • 不要直接在ASPX标记中编写. net代码(除非是用于数据绑定的,比如Evals)。如果您有一个代码,这就会在多个地方放置一个页面的代码,并使代码更容易管理。将所有的。net代码放在代码后。
  • SessionPageStatePersister can be used in conjunction with ViewState to make ViewState useful without increasing page sizes. Overriding the Page's PageStatePersister with a new SessionPageStatePersister will store all ViewState data in memory, and will only store an encrypted key on the client side.
  • SessionPageStatePersister可以与ViewState一起使用,使ViewState在不增加页面大小的情况下变得有用。用一个新的SessionPageStatePersister重写页面的PageStatePersister,它将在内存中存储所有的ViewState数据,并且只在客户端存储一个加密的密钥。
  • Create a BasePage that your pages can inherit from in order to reuse common code between pages. Create a MasterPage for your pages for visual inheritance. Pages with vastly different visual styles should use a different MasterPage.
  • 创建页面可以继承的BasePage,以便在页面之间重用公共代码。为页面创建一个主页以实现视觉继承。具有不同视觉风格的页面应该使用不同的主页。
  • Create an enum of page parameter key names on each WebForm that are passed in via the URL, to setup strongly-typed page parameters. This prevents the need for hard-coded page parameter key strings and their probable mis-typing, as well as allowing strongly-typed parameter access from other pages.
  • 在通过URL传入的每个web表单上创建一个页面参数键名的枚举,以设置强类型的页面参数。这避免了需要硬编码的页面参数键字符串及其可能的错误输入,以及允许从其他页面访问强类型的参数。
  • Make use of the ASP.NET Cache in order to cache frequently used information from your database. Build (or reuse from another project) a generic caching layer that will wrap the ASP.NET Cache.
  • 利用ASP。NET缓存,以便从数据库缓存经常使用的信息。构建(或从另一个项目重用)将包装ASP的通用缓存层。网络缓存。
  • Wrap ViewState objects with Properties on your Pages to avoid development mistakes in spelling, etc. when referencing items from the ViewState collection.
  • 在引用ViewState集合中的项时,将ViewState对象包装为页面上的属性,以避免在拼写等方面出现开发错误。
  • Avoid putting large objects and object graphs in ViewState, use it mainly for storing IDs or very simple DTO objects.
  • 避免将大型对象和对象图放在ViewState中,主要用于存储id或非常简单的DTO对象。
  • Wrap the ASP.NET Session with a SessionManager to avoid development mistakes in spelling, etc. when referencing items from Session.
  • 包装的ASP。与SessionManager的NET会话,以避免在引用会话中的项时在拼写等方面出现开发错误。
  • Make extensive use of the applicationSettings key/value configuration values in the web.config - wrap the Configuration.ApplicationSettings with a class that can be used to easily retrieve configuration settings without having to remember the keys from the web.config.
  • 在web上广泛使用应用程序设置键/值配置值。配置——包装配置。ApplicationSettings包含一个类,可以用来轻松检索配置设置,而不需要记住web.config中的键。
  • Avoid the easiness of setting display properties on your UI controls, instead use CSS styles and classes - this will make your styles more manageable.
  • 避免在UI控件上设置显示属性,而是使用CSS样式和类——这将使您的样式更易于管理。
  • Create UserControls in your application in order to reuse common UI functionality throughout your pages. For example, if a drop down list containing a collection of categories will be used in many places in the site - create a CategoryPicker control that will data bind itself when the page is loaded.
  • 在应用程序中创建用户控件,以便在整个页面中重用公共UI功能。例如,如果包含类别集合的下拉列表将在站点的许多地方使用——创建一个CategoryPicker控件,该控件将在页面加载时绑定自己的数据。
  • Use Properties on your UserControls to setup things like default values, different displays between pages, etc. Value type properties can be defined on your UserControls and then be set in your ASP.NET markup by using class level properties on UserControls.
  • 在用户控件上使用属性设置默认值、页面之间的不同显示等等。值类型属性可以在用户控件上定义,然后在ASP中设置。在UserControls中使用类级属性的净标记。
  • Make use of the ASP.NET validation controls to perform simple validations, or use the CustomValidator to perform complex validations.
  • 利用ASP。NET验证控件执行简单的验证,或者使用CustomValidator执行复杂的验证。
  • Create an Error handling page that can be redirected to when an unhandled exception occurs within your website. The redirection can occur via the Page_Error event in your Page, the Application_Error event in your Global.asax, or within the section within the web.config.
  • 创建一个错误处理页面,当网站中出现未处理的异常时可以重定向到该页面。重定向可以通过页面中的Page_Error事件、全局中的Application_Error事件发生。asax,或web.config中的部分。
  • When working with pages that use a highly dynamic data driven display, use the 3rd party (free) DynamicControlsPlaceholder control to simplify the code needed to save the state of dynamically added controls between postbacks.
  • 使用高度动态数据驱动显示的页面时,使用第三方(免费)DynamicControlsPlaceholder控件来简化保存回发之间动态添加控件状态所需的代码。

#2


19  

  1. Create a base page for all your asp.net pages. This page will derive from System.Web.UI.Page and you may put this in YourApp.Web.UI. Let all your asp.net pages dervice from YourApp.Web.UI.Page class. This can reduce lot of pain.

    为所有asp.net页面创建一个基础页面。这个页面将派生自System.Web.UI。你可以把这个放到你的app。web。ui中。让你的asp.net页面从你的app . web . ui中删除。页面类。这可以减少很多痛苦。

  2. Use Application_OnError handler to gracefully handle any error or exception. You should log the critical exception and send the details of the exception along with date-time and IP of client to the admin email id. Yes ELMAH is sure way to go.

    使用Application_OnError处理程序优雅地处理任何错误或异常。您应该记录关键异常,并将异常的详细信息连同客户的日期时间和IP发送到管理电子邮件id。

  3. Use ASP.NET Themes. Many developers don't use it. Do use them - they are a great deal.

    使用ASP。净的主题。许多开发人员不使用它。一定要使用它们——它们非常有用。

  4. Use MembershipProvider and RoleProvider. And Never use inbuilt ProfileProvider - They store everything in plain strings. It will drastically slow-down the performance while performing R/W

    使用MembershipProvider和RoleProvider类。不要使用内置的ProfileProvider——它们将所有内容存储在纯字符串中。它将在执行R/W时显著降低性能

  5. Use Firebug for client-side debugging. Try to follow YSlow standards for web-applications. Use YSlow extension for FireBug.

    使用Firebug进行客户端调试。尝试遵循web应用程序的YSlow标准。对FireBug使用YSlow扩展。

  6. Use jQuery for client-scripting.

    对客户端脚本使用jQuery。

  7. Never store User Authentication information in session or don't use sessions to judge if user is logged on. Store only minimum necessary information in sessions.

    不要在会话中存储用户身份验证信息,也不要使用会话来判断用户是否登录。在会话中只存储必要的最小信息。

  8. Have a look at PostSharp. Can improve maintainability of your code and make you more productive.

    看一下PostSharp。可以提高代码的可维护性,使您的工作效率更高。

  9. Never ever Deploy asp.net application under debug configuration on production. Find out here what scottgu has to say about this.

    永远不要在生产的调试配置下部署asp.net应用程序。看看斯科特古是怎么说的。

  10. User Web Deployment projects. It can transform web.config sections and replace with production server setings. It will merge all compiled code-behind classes into one single assembly which is a great deal.

    用户Web部署项目。它可以变换网络。配置部分并替换为生产服务器的设置。它将把所有编译后的代码隐藏类合并到一个单独的程序集中,这是非常重要的。

  11. Use Cookie-less domains to serve static resources like images, scripts, styles etc. Each client request is sent along with whole bunch of cookies, you don't need cookies while serving pictures or scripts. So host those resources on a cookie-less domain.

    使用无cookie的域来服务静态资源,如图像、脚本、样式等。每个客户端请求都随一系列cookie一起发送,在提供图片或脚本时不需要cookie。因此,将这些资源托管在一个无cookie的域中。

  12. Minify scripts, stylesheets and HTML response from the server. Removing unnecessary line-breaks and white-spaces can improve the time-to-load and bandwidth optimization.

    缩小脚本、样式表和来自服务器的HTML响应。消除不必要的断行和空白可以改进加载时间和带宽优化。

#3


7  

Forms:

形式:

  1. Set Page.Form.DefaultFocus and Page.Form.DefaultButton to improve user experience

    设置Page.Form。DefaultFocus Page.Form。DefaultButton以改进用户体验

  2. Check Page.IsValid in your Save button handler before proceeding.

    检查页面。在继续之前,保存按钮处理程序是有效的。

General:

一般:

  1. Understand and implement the techniques found in the article "TRULY Understanding ViewState"

    理解并实现文章中发现的“真正理解ViewState”的技术

  2. Use Page.IsPostBack in your page events to stop code from running unnecessarily.

    使用页面。IsPostBack用于阻止不必要的代码运行。

  3. Use hyperlinks instead of posting and using Response.Redirect whenever possible.

    使用超链接而不是发布和使用响应。重定向。

    a. Understand and use the second parameter of Response.Redirect (it "Indicates whether execution of the current page should terminate")

    a.理解并使用响应的第二个参数。重定向(它“指示当前页的执行是否应该终止”)

  4. Use the Page Lifecycle properly.

    正确使用页面生命周期。

  5. Use the Per-Request cache (HttpContext.Items) instead of Cache where it makes sense.

    使用每个请求缓存(HttpContext.Items),而不是在有意义的地方缓存。

Web.Config:

. config:

  1. Deploy with <compilation debug="false">

    与 <编译部署调试= " false>

  2. Register your controls at the web.config level instead of the page level (i.e. @Register).

    在web上注册控件。配置级别而不是页面级别(例如@Register)。

Themes:

主题:

  1. When using Themes, put your static images in the Theme as well.

    当使用主题时,将静态图像放在主题中。

    a. Don't link to the images directly from your markup, link to them from a skin file or css file in your Theme instead.

    a.不要直接从标记链接到图像,而是从主题中的皮肤文件或css文件链接到它们。

    ex: <asp:Image SkinID="MyImage" runat="server" ImageUrl="Images/myImage.gif" />

    例:

#4


4  

I don't think try/catch blocks are always appropriate for low-level methods. You want to catch (and log/alert, even better!) any errors before they get to the user, of course. But it is often better for a low-level method to just lets its errors get raised up to a higher level. The problem I have seen with low-level error trapping is that it often lets a larger operation continue, but then a result that is not quite correct gets presented to the user or saved to the database, and in the long run it's much more difficult to fix. It's also just a lot of extra clutter in your code to put a try/catch at every level if you're not going to "do anything" with the error until it's raised up to a higher level.

我认为try/catch块并不总是适合低级方法。当然,您希望在错误到达用户之前捕获(以及日志/警报)任何错误。但是对于一个低级的方法来说,最好是让它的错误被提升到更高的级别。我所见过的低级错误捕获的问题是,它通常会让更大的操作继续,但随后不太正确的结果会呈现给用户或保存到数据库中,从长远来看,修复起来要困难得多。如果您不打算对错误“做任何事情”,直到错误被提升到更高的级别,那么在您的代码中添加每一层的try/catch也会造成很多额外的混乱。

#5


2  

Here are some similar questions that may help you.

这里有一些类似的问题可以帮助你。

.NET best practices?

net的最佳实践?

Best way to learn .NET/OOP best practices?

学习.NET/OOP最佳实践的最佳方式?

This should probably be community wiki as well.

这应该也是社区wiki。

#6


1  

I would recommend a couple of books if you are interested in pursuing a journey to become a better, more productive developer. These books are language agnostic and as you can see by the user reviews, very very helpful.

如果你想成为一个更好、更高效的开发人员,我建议你读几本书。这些书是语言不可知论的,你可以从用户评论中看到,非常有用。

Code Complete 2

代码完成2

Pragmatic Programmer

务实程序员

If you are looking for a .NET specific book, you may appreciate the following book:

如果你正在寻找一本。net特定的书,你可能会喜欢以下的书:

Microsoft Application Architecture Guide [available online for free outside of print format]

微软应用程序架构指南[可在网上免费获取打印格式以外的内容]

#7


1  

ASP.NET

ASP.NET

  • If you don't use Session state, don't forget to turn off it.
  • 如果不使用会话状态,不要忘记关闭它。
  • Use Server.Transfer instead of Response.Redirect if possible.
  • 使用服务器。转移,而不是回应。如果可能的话,重定向。
  • Set expiration parameter in IIS.
  • 在IIS中设置过期参数。
  • Use GZip to compress the text files.
  • 使用GZip压缩文本文件。
  • Use Server-Side and Client-Side validation together.
  • 同时使用服务器端和客户端验证。
  • Use Url Rewriter or Routing to make friendly url for SEO.
  • 使用Url重写器或路由为SEO创建友好的Url。

Design

设计

  • Write each class and its properties of your CSS file in the same line. (To decrease the file size)
  • 在同一行中编写CSS文件的每个类及其属性。(减少档案大小)
  • Use CSS Sprites. (To decrease request)
  • 使用CSS精灵。(减少请求)