如何提高ASP.NET MVC应用程序性能
如何提高你的ASP.NET MVC应用程序的性能?
可能的改进来源编译的名单是如下:
A compiled list of possible sources of improvement are below:
常规
- 请使用探查,发现在应用程序的内存泄漏和性能问题。我个人建议 dotTrace
- 运行在发行模式,而不是调试模式下你的网站,如果在生产中,也时性能分析。释放模式要快得多。调试模式可以在自己的code隐藏的性能问题。
缓存
- 使用
CompiledQuery.Compile()
递归地避免 您的查询的重新编译 EX pressions - 在缓存不倾向对变革
使用含量
OutputCacheAttribute
节省不必要和行动 执行 - 使用Cookie用于经常访问的非敏感信息
- 在利用 ETag的和到期 - 编写自定义
的ActionResult
方法,如果需要的话 - 在考虑使用
RouteName
来组织你的路线,然后用它来生成 您的链接,并尽量不要使用前pression基于树的ActionLink的方法。 - 在考虑实施一个路由解析缓存策略
- 将重复code在你的
PartialViews
,避免使它的 XXXX 的时间:如果你 最终调用相同的部分300倍,在同样的观点,可能有一些 错。 Explanation而基准
- 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
路由
-
使用以
Url.RouteUrl(用户,新{用户名=为JoeUser})
指定的路线。 ASP.NET MVC性能比较由鲁迪Benkovic
缓存航线使用这个辅助解决 UrlHelperCached
ASP.NET MVC性能比较由鲁迪Benkovic
Cache route resolving using this helper UrlHelperCached
ASP.NET MVC Perfomance by Rudi Benkovic
安全
- 使用窗体身份验证,请您经常访问敏感数据 身份验证票证
- Use Forms Authentication, Keep your frequently accessed sensitive data in the authentication ticket
DAL
- 在访问通过LINQ 依靠IQueryable的 在当前
- Leverage Repository模式
- 资料查询即尤伯杯探查
- 在考虑二级缓存查询,并将其添加的范围和超时即NHibernate其次缓存
- 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
负载均衡
-
利用反向代理,送$ P $垫在你的应用程序实例的客户端负载。 (堆栈溢出使用 HAProxy的(MSDN).
使用异步控制器来实施行动依赖于外部资源的处理。
Use Asynchronous Controllers to implement actions that depend on external resources processing.
客户端
- 优化您的客户端,使用像 YSlow的的一种工具 建议提高性能
- 使用AJAX来更新你的UI组件,避免整个页面的更新时可能。
- 在考虑实施一个发布 - 订阅架构 - 即Comet-反对内容交付 重装总部设在超时。
- 移动的图表和图形生成逻辑到客户端可能的话。图生成 是一个昂贵的活动。从推迟到客户端服务器 不必要的负担,并允许您使用图形来本地工作没有作出新的 请求(即Flex图表, jqbargraph ,MoreJqueryCharts).
- 使用CDN的脚本和媒体内容,以提高装载在客户端(即谷歌CDN )
- 在缩小 - 编译 - 你的,以提高你的脚本尺寸的JavaScript
- 在保持cookie的尺寸不大,因为cookie被发送到每个请求的服务器。
- 在考虑使用 DNS和链接prefetching 在可能的情况。
- 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.
全局配置
-
如果您使用的剃刀,添加以下code在你的global.asax.cs,默认情况下,Asp.Net MVC有一个aspx发动机和剃刀引擎渲染。这种仅使用RazorViewEngine。
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(新RazorViewEngine());
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
添加gzip压缩(HTTP COM pression)和静态缓存(图像,CSS,...)在你的web.config
< system.webServer>
< urlCom pression doDynamicCom pression =真doStaticCom pression =真dynamicCom pressionBeforeCache =真/>
< /system.webServer>
Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
</system.webServer>