如何提高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">
其他回答
只是想说说我的意见。优化MVC应用程序中URL路由生成的最有效的方法是…根本不产生它们。
我们大多数人或多或少都知道url是如何在我们的应用程序中生成的,所以只要使用静态Url.Content(“~/Blahblah”)而不是Url.Action()或Url.RouteUrl(),就可以胜过所有其他方法近20倍甚至更多。
PS:我已经运行了几千次迭代的基准测试,如果有兴趣的话可以在我的博客上发布结果。
使用最新版本的任务并行库(TPL),根据. net版本。必须根据不同的用途选择正确的TPL模块。
使用捆绑和缩小还可以帮助您提高性能。它基本上减少了页面加载时间。
另外,如果你使用NHibernate,你可以为查询打开和设置二级缓存,并添加到查询范围和超时。并且有踢屁股的EF, L2S和NHibernate分析器- http://hibernatingrhinos.com/products/UberProf。这将有助于调优您的查询。
如果您正在运行您的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.
推荐文章
- 是什么导致JNI调用变慢?
- 如何从字符串中删除新的行字符?
- 检查属性是否有属性
- 如何快速清除JavaScript对象?
- jQuery Ajax调用和Html.AntiForgeryToken()
- 是否已经添加了事件处理程序?
- Lookup()和Dictionary(Of list()的区别
- 如何从我的c#代码运行EXE文件?
- Node.js vs .Net性能
- 如果使用if-return-return或if-else-return?
- .NET控制台应用程序中的全局异常处理程序
- 枚举上最常见的c#位操作
- PowerShell脚本在机器上返回。net框架的版本?
- 用c#创建一个空文件
- 如何改变列表<T>数据IQueryable<T>数据