更新时间:2022-10-18 19:24:00
以下列出了可能的改进来源:
一般
缓存
CompiledQuery.Compile()
递归避免重新编译您的查询表达OutputCacheAttribute
的内容节省不必要的和行动处决ActionResult
方法(如有必要)RouteName
来组织您的路线,然后使用它来生成您的链接,并尽量不要使用基于表达式树的 ActionLink 方法.PartialViews
中,避免渲染它 xxxx 次:如果你最终在同一个视图中调用同一个部分 300 次,可能有一些东西错了.解释和基准
路由
使用 Url.RouteUrl("User", new { username = "joeuser" })
指定路由.ASP.NET MVC 性能作者 Rudi Benkovic
缓存路由解析使用这个助手 UrlHelperCached
ASPRudi Benkovic 的 .NET MVC 性能
安全
DAL
负载平衡
客户端
全局配置
如果您使用 Razor,请在 global.asax.cs 中添加以下代码,默认情况下,Asp.Net MVC 使用 aspx 引擎和 razor 引擎进行渲染.这仅使用 RazorViewEngine.
ViewEngines.Engines.Clear();ViewEngines.Engines.Add(new RazorViewEngine());
在您的 web.config 中添加 gzip(HTTP 压缩)和静态缓存(图像、CSS 等)
How do you improve your ASP.NET MVC application performance?
A compiled list of possible sources of improvement are below:
General
Caching
CompiledQuery.Compile()
recursively avoiding
recompilation of your query
expressionsOutputCacheAttribute
to save unnecessary and action
executionsActionResult
methods if necessaryRouteName
to organize your routes and then use it to generate
your links, and try not to use the expression tree based ActionLink method. 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
Routing
Use Url.RouteUrl("User", new { username = "joeuser" })
to specify routes. ASP.NET MVC Perfomance by Rudi Benkovic
Cache route resolving using this helper UrlHelperCached
ASP.NET MVC Perfomance by Rudi Benkovic
Security
DAL
Load balancing
Utilize reverse proxies, to spread the client load across your app instance. (Stack Overflow uses HAProxy (MSDN).
Use Asynchronous Controllers to implement actions that depend on external resources processing.
Client side
Global configuration
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>
<pages buffer="true" enableViewState="false">