如何提高 ASP.NET MVC 应用程序的性能? [英] How do I improve ASP.NET MVC application performance?

查看:40
本文介绍了如何提高 ASP.NET MVC 应用程序的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何提高 ASP.NET MVC 应用程序的性能?

解决方案

以下列出了可能的改进来源:

一般

  • 使用分析器来发现应用程序中的内存泄漏和性能问题.我个人建议 dotTrace
  • 在生产和性能分析期间,以发布模式而非调试模式运行您的网站.释放模式要快得多.调试模式可以在您自己的代码中隐藏性能问题.

缓存

  • 使用CompiledQuery.Compile()递归避免重新编译您的查询表达
  • 缓存不易更改使用 OutputCacheAttribute 的内容节省不必要的和行动处决
  • 将 cookie 用于经常访问的非敏感信息
  • 利用 ETags 和过期 - 编写您的自定义ActionResult 方法(如有必要)
  • 考虑使用 RouteName 来组织您的路线,然后使用它来生成您的链接,并尽量不要使用基于表达式树的 ActionLink 方法.
  • 考虑实施路由解析缓存策略
  • 将重复的代码放在你的 PartialViews 中,避免渲染它 xxxx 次:如果你最终在同一个视图中调用同一个部分 300 次,可能有一些东西错了.解释和基准

路由

安全

  • 使用表单身份验证,将您经常访问的敏感数据保存在身份验证票

DAL

负载平衡

  • 利用反向代理,将客户端负载分散到您的应用程序实例中.(堆栈溢出使用 HAProxy (MSDN).

  • 使用异步控制器 实施依赖于外部资源处理的操作.

客户端

  • 优化您的客户端,使用YSlow之类的工具提高性能的建议
  • 使用 AJAX 更新您的 UI 组件,尽可能避免更新整个页面.
  • 考虑实施发布-订阅架构 - 即Comet-针对内容交付基于超时重新加载.
  • 如果可能,将图表和图形生成逻辑移至客户端.图生成是一项昂贵的活动.将服务器从客户端推迟到客户端不必要的负担,并允许您在本地处理图形而无需创建新的请求(即 Flex 图表、jqbargraphMoreJqueryCharts).
  • 将 CDN 用于脚本和媒体内容以改善客户端的加载(即 Google CDN)
  • 缩小 -编译- 您的 JavaScript 以改善脚本大小
  • 保持较小的 cookie 大小,因为每次请求都会将 cookie 发送到服务器.
  • 考虑在可能的情况下使用DNS 和链接预取.

全局配置

  • 如果您使用 Razor,请在 global.asax.cs 中添加以下代码,默认情况下,Asp.Net MVC 使用 aspx 引擎和 razor 引擎进行渲染.这仅使用 RazorViewEngine.

    ViewEngines.Engines.Clear();ViewEngines.Engines.Add(new RazorViewEngine());

  • 在您的 web.config 中添加 gzip(HTTP 压缩)和静态缓存(图像、CSS 等)<代码><urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/></system.webServer>

  • 删除未使用的 HTTP 模块
  • 在生成 HTML 后立即刷新(在您的 web.config 中),如果您不使用它,请禁用视图状态

How do you improve your ASP.NET MVC application performance?

解决方案

A compiled list of possible sources of improvement are below:

General

  • Make use of a profiler to discover memory leaks and performance problems in your application. personally I suggest dotTrace
  • Run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.

Caching

  • Use CompiledQuery.Compile() recursively avoiding recompilation of your query expressions
  • Cache not-prone-to-change content using OutputCacheAttribute to save unnecessary and action executions
  • Use cookies for frequently accessed non sensitive information
  • Utilize ETags and expiration - Write your custom ActionResult methods if necessary
  • Consider using the RouteName to organize your routes and then use it to generate your links, and try not to use the expression tree based ActionLink method.
  • Consider implementing a route resolution caching strategy
  • Put repetitive code inside your PartialViews, avoid render it xxxx times: if you end up calling the same partial 300 times in the same view, probably there is something wrong with that. Explanation And Benchmarks

Routing

Security

  • Use Forms Authentication, Keep your frequently accessed sensitive data in the authentication ticket

DAL

Load balancing

  • Utilize reverse proxies, to spread the client load across your app instance. (Stack Overflow uses HAProxy (MSDN).

  • Use Asynchronous Controllers to implement actions that depend on external resources processing.

Client side

  • Optimize your client side, use a tool like YSlow for suggestions to improve performance
  • Use AJAX to update components of your UI, avoid a whole page update when possible.
  • Consider implement a pub-sub architecture -i.e. Comet- for content delivery against reload based in timeouts.
  • Move charting and graph generation logic to the client side if possible. Graph generation is a expensive activity. Deferring to the client side your server from an unnecessary burden, and allows you to work with graphs locally without make a new request (i.e. Flex charting, jqbargraph, MoreJqueryCharts).
  • Use CDN's for scripts and media content to improve loading on the client side (i.e. Google CDN)
  • Minify -Compile- your JavaScript in order to improve your script size
  • Keep cookie size small, since cookies are sent to the server on every request.
  • Consider using DNS and Link Prefetching when possible.

Global configuration

  • If you use Razor, add the following code in your global.asax.cs, by default, Asp.Net MVC renders with an aspx engine and a razor engine. This only uses the RazorViewEngine.

    ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine());

  • Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config <system.webServer> <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/> </system.webServer>

  • Remove unused HTTP Modules
  • Flush your HTML as soon as it is generated (in your web.config) and disable viewstate if you are not using it <pages buffer="true" enableViewState="false">

这篇关于如何提高 ASP.NET MVC 应用程序的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆