如何提高ASP ?NET MVC应用程序性能?
当前回答
一件非常简单的事情是在访问页面所需的数据时进行异步思考。无论是从web服务、文件、数据库还是其他东西读取,都尽可能使用异步模型。虽然它不一定会帮助任何一个页面更快,但它会帮助您的服务器整体性能更好。
其他回答
使用捆绑和缩小还可以帮助您提高性能。它基本上减少了页面加载时间。
当通过LINQ访问数据依赖IQueryable…
为什么使用AsQueryable()而不是List()?
... 并利用一个好的存储库模式:
在存储库模式中加载子记录
这将优化数据访问,以确保只加载需要的数据。
另外,如果你使用NHibernate,你可以为查询打开和设置二级缓存,并添加到查询范围和超时。并且有踢屁股的EF, L2S和NHibernate分析器- http://hibernatingrhinos.com/products/UberProf。这将有助于调优您的查询。
这不是一个惊天动地的优化,但我想我要把它扔在那里-使用CDN的jQuery,等等。
引用ScottGu自己的话:微软Ajax CDN使你能够显著提高ASP的性能。NET Web Forms和ASP。asp.net MVC应用程序使用ASP。NET AJAX或jQuery。这项服务是免费的,不需要任何注册,可以用于商业和非商业目的。
我们甚至使用CDN为我们的web部件在Moss使用jQuery。
以下是可能的改进来源:
一般
利用分析器来发现应用程序中的内存泄漏和性能问题。我个人建议dotTrace 在生产环境和性能分析期间,以发布模式(而不是调试模式)运行站点。释放模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。
缓存
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("User", new {username = "joeuser"})指定路由。ASP。NET MVC性能由Rudi Benkovic编写 缓存路由解析使用这个助手UrlHelperCached ASP。NET MVC性能由Rudi Benkovic编写
安全
使用表单身份验证,将您经常访问的敏感数据保存在 身份验证票
DAL
当通过LINQ访问数据时,依赖于IQueryable 利用存储库模式 配置您的查询,即优步Profiler 考虑二级缓存,为你的查询添加一个范围和一个超时,即NHibernate二级缓存
负载平衡
利用反向代理,在应用实例中分散客户端负载。Stack Overflow使用HAProxy (MSDN)。 使用异步控制器来实现依赖于外部资源处理的操作。
客户端
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.
全局配置
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">
推荐文章
- c# .NET中的App.config是什么?如何使用它?
- 如何找到Java堆大小和内存使用(Linux)?
- 在哪里放置AutoMapper.CreateMaps?
- String类中的什么方法只返回前N个字符?
- 如何在HTML5中改变视频的播放速度?
- 我如何提高ASP。NET MVC应用程序性能?
- 无法解析类型为“Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务
- 如何在没有任何错误或警告的情况下找到构建失败的原因
- Visual Studio弹出提示:“操作无法完成”
- 否ConcurrentList<T>在。net 4.0?
- 在c#中解析字符串为日期时间
- 如何在剃刀视图上引用.css文件?
- 由Jon Skeet撰写的《Singleton》澄清
- 自定义数字格式字符串始终显示符号
- 列表推导式和函数式函数比for循环更快吗?