问题描述
您如何提高 ASP.NET MVC 应用程序的性能?
以下列出了可能的改进来源:
一般
- 使用分析器来发现应用程序中的内存泄漏和性能问题.我个人建议 dotTrace
- 在生产和性能分析期间,以发布模式而非调试模式运行您的网站.释放模式要快得多.调试模式可以在您自己的代码中隐藏性能问题.
缓存
- 使用
CompiledQuery.Compile()
递归避免重新编译您的查询表达 - 缓存不易更改使用
OutputCacheAttribute
的内容节省不必要的和行动处决 - 将 cookie 用于经常访问的非敏感信息
- 利用 ETags 和过期 - 编写您的自定义
ActionResult
方法(如有必要) - 考虑使用
RouteName
来组织您的路线,然后使用它来生成您的链接,并尽量不要使用基于表达式树的 ActionLink 方法. - 考虑实施路由解析缓存策略
- 将重复的代码放在你的
PartialViews
中,避免渲染它 xxxx 次:如果你最终在同一个视图中调用同一个部分 300 次,可能有一些东西错了.解释和基准
路由
使用
Url.RouteUrl("User", new { username = "joeuser" })
指定路由.ASP.NET MVC 性能作者 Rudi Benkovic缓存路由解析使用这个助手
UrlHelperCached
ASPRudi Benkovic 的 .NET MVC 性能
安全
- 使用表单身份验证,将您经常访问的敏感数据保存在身份验证票
DAL
- 通过 LINQ 访问数据时依赖 IQueryable
- 利用存储库模式
- 分析您的查询,例如 Uber Profiler
- 考虑为您的查询设置二级缓存,并为它们添加范围和超时,即 NHibernate 二级缓存
负载平衡
客户端
- 优化您的客户端,使用YSlow之类的工具提高性能的建议
- 使用 AJAX 更新您的 UI 组件,尽可能避免更新整个页面.
- 考虑实施发布-订阅架构 - 即Comet-针对内容交付基于超时重新加载.
- 如果可能,将图表和图形生成逻辑移至客户端.图生成是一项昂贵的活动.将服务器从客户端推迟到客户端不必要的负担,并允许您在本地处理图形而无需创建新的请求(即 Flex 图表、jqbargraph、MoreJqueryCharts).
- 将 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 avoidingrecompilation of your queryexpressions - Cache not-prone-to-changecontent using
OutputCacheAttribute
to save unnecessary and actionexecutions - 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 generateyour 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 youend up calling the same partial 300 times in the same view, probably there is somethingwrong with that. Explanation And Benchmarks
Routing
Use
Url.RouteUrl("User", new { username = "joeuser" })
to specify routes. ASP.NET MVC Perfomance by Rudi BenkovicCache route resolving using this helper
UrlHelperCached
ASP.NET MVC Perfomance by Rudi Benkovic
Security
- Use Forms Authentication, Keep your frequently accessed sensitive data in theauthentication ticket
DAL
- When accessing data via LINQ rely on IQueryable
- Leverage the Repository pattern
- Profile your queries i.e. Uber Profiler
- Consider second level cache for your queries and add them an scope and a timeout i.e. NHibernate Second Cache
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 forsuggestions 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 againstreload based in timeouts.
- Move charting and graph generation logic to the client side if possible. Graph generationis a expensive activity. Deferring to the client side your server from anunnecessary burden, and allows you to work with graphs locally without make a newrequest (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 应用程序的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!