如何提高ASP ?NET MVC应用程序性能?
当前回答
使用捆绑和缩小还可以帮助您提高性能。它基本上减少了页面加载时间。
其他回答
除了所有关于在服务器端优化应用程序的重要信息外,我还想说你应该看看YSlow。它是提高客户端站点性能的极好的资源。
这适用于所有网站,而不仅仅是ASP。净MVC。
这似乎是显而易见的,但是在生产环境中以及在性能分析期间以发布模式而不是调试模式运行站点。释放模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。
以下是可能的改进来源:
一般
利用分析器来发现应用程序中的内存泄漏和性能问题。我个人建议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">
Code Climber和这篇博客文章提供了提高应用程序性能的详细方法。
编译后的查询将提高应用程序的性能,但它与ASP没有任何共同之处。净MVC。它将加速每一个db应用程序,所以它不是真正的MVC。
我做了上面所有的答案,但还是没有解决我的问题。
最后,我解决了我的网站加载缓慢的问题,在发布配置文件PrecompileBeforePublish设置为真。如果你想使用msbuild,你可以使用这个参数:
/p:PrecompileBeforePublish=true
这真的很有帮助。现在我的MVC ASP。NET加载速度快10倍。
推荐文章
- 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循环更快吗?