如何提高ASP ?NET 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。
如果您正在运行您的ASP。NET MVC应用程序在Microsoft Azure (IaaS或PaaS)上,然后至少在第一次部署之前执行以下操作。
Scan your code with static code analyzer for any type of code debt, duplication, complexity and for security. Always enable the Application Insight, and monitor the performance, browsers, and analytics frequently to find the real-time issues in the application. Implement Azure Redis Cache for static and less frequent change data like Images, assets, common layouts etc. Always rely on APM (Application Performance Management) tools provided by Azure. See application map frequently to investigate the communication performance between internal parts of the application. Monitor Database/VM performance too. Use Load Balancer (Horizontal Scale) if required and within the budget. If your application has the target audience all over the globe, then use Azure Trafic Manager to automatically handle the incoming request and divert it to the most available application instance. Try to automate the performance monitoring by writing the alerts based on low performance.
我做了上面所有的答案,但还是没有解决我的问题。
最后,我解决了我的网站加载缓慢的问题,在发布配置文件PrecompileBeforePublish设置为真。如果你想使用msbuild,你可以使用这个参数:
/p:PrecompileBeforePublish=true
这真的很有帮助。现在我的MVC ASP。NET加载速度快10倍。
一件非常简单的事情是在访问页面所需的数据时进行异步思考。无论是从web服务、文件、数据库还是其他东西读取,都尽可能使用异步模型。虽然它不一定会帮助任何一个页面更快,但它会帮助您的服务器整体性能更好。
Gzip实现。 对部分视图使用异步呈现。 最小化数据库命中次数。 使用编译后的查询。 运行分析器找出不必要的匹配。优化所有返回响应时间超过1秒的存储过程。 使用缓存。 使用捆绑最小化优化。 对只读内容使用会话缓存和本地存储等HTML 5实用程序。
推荐文章
- 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循环更快吗?