如何提高ASP ?NET MVC应用程序性能?


当前回答

基本的建议是遵循REST原则,以下几点将这些原则中的一些与ASP。NET MVC框架:

Make your controllers stateless - this is more of a 'Web performance / scalability' suggestion (as opposed to micro/machine level performance) and a major design decision that would affect your applications future - especially in case it becomes popular or if you need some fault tolerance for example. Do not use Sessions Do not use tempdata - which uses sessions Do not try to 'cache' everything 'prematurely'. Use Forms Authentication Keep your frequently accessed sensitive data in the authentication ticket Use cookies for frequently accessed non sensitive information Make your resources cachable on the web Utilize ETags Use expiration Write your custom ActionResult classes if necessary Utilize reverse proxies Compile your JavaScript. There is Closure compiler library to do it as well (sure there are others, just search for 'JavaScript compiler' too) Use CDNs (Content Delivery Network) - especially for your large media files and so on. Consider different types of storage for your data, for example, files, key/value stores, etc. - not only SQL Server Last but not least, test your web site for performance

其他回答

Code Climber和这篇博客文章提供了提高应用程序性能的详细方法。

编译后的查询将提高应用程序的性能,但它与ASP没有任何共同之处。净MVC。它将加速每一个db应用程序,所以它不是真正的MVC。

Gzip实现。 对部分视图使用异步呈现。 最小化数据库命中次数。 使用编译后的查询。 运行分析器找出不必要的匹配。优化所有返回响应时间超过1秒的存储过程。 使用缓存。 使用捆绑最小化优化。 对只读内容使用会话缓存和本地存储等HTML 5实用程序。

1:获取时间。在你知道放缓在哪里之前,这个问题太宽泛了,无法回答。我正在做的一个项目就有这样的问题;没有日志记录,甚至不知道某些事情需要多长时间;在我们将计时添加到项目中之前,我们只能猜测应用程序的慢部分。

2:如果你有顺序操作,不要害怕轻度多线程。特别是当涉及到阻塞操作时。PLINQ是你的朋友。

3:在发布时预生成MVC视图这将有助于一些“第一页点击”

4:有些人认为存储过程/ADO在速度上有优势。其他人则主张加快英孚教育的发展速度,并对教育层级及其目的进行更明确的划分。我曾见过SQL和使用scpros /Views进行数据检索和存储的变通方法时非常缓慢的设计。同时,你的测试难度也会增加。我们目前正在从ADO转换到EF的代码库的性能并不比旧的手卷模型差(在某些情况下更好)。

5:也就是说,考虑一下应用程序的预热。我们所做的帮助消除大部分EF性能问题的部分工作是添加一个特殊的预热方法。它不预编译任何查询或任何东西,但它有助于大量元数据的加载/生成。在处理Code First模型时,这一点可能更加重要。

6: As others have said, Don't use Session state or ViewState if possible. They are not necessarily performance optimizations that developers think about, but once you start writing more complex web applications, you want responsiveness. Session state precludes this. Imagine a long running query. You decide to open a new window and try a less complex one. Well, you may as well have waited with session state on, because the server will wait until the first request is done before moving to the next one for that session.

7:最小化到数据库的往返。保存你经常使用但实际上不会更改到. net缓存的东西。在可能的情况下尝试批处理插入/更新。

7.1:避免数据访问代码在你的Razor视图没有一个该死的好理由。要不是亲眼所见,我也不会这么说。在构建模型时,他们已经在访问数据了,为什么他们不把数据包括在模型中呢?

只是想说说我的意见。优化MVC应用程序中URL路由生成的最有效的方法是…根本不产生它们。

我们大多数人或多或少都知道url是如何在我们的应用程序中生成的,所以只要使用静态Url.Content(“~/Blahblah”)而不是Url.Action()或Url.RouteUrl(),就可以胜过所有其他方法近20倍甚至更多。

PS:我已经运行了几千次迭代的基准测试,如果有兴趣的话可以在我的博客上发布结果。

基本的建议是遵循REST原则,以下几点将这些原则中的一些与ASP。NET MVC框架:

Make your controllers stateless - this is more of a 'Web performance / scalability' suggestion (as opposed to micro/machine level performance) and a major design decision that would affect your applications future - especially in case it becomes popular or if you need some fault tolerance for example. Do not use Sessions Do not use tempdata - which uses sessions Do not try to 'cache' everything 'prematurely'. Use Forms Authentication Keep your frequently accessed sensitive data in the authentication ticket Use cookies for frequently accessed non sensitive information Make your resources cachable on the web Utilize ETags Use expiration Write your custom ActionResult classes if necessary Utilize reverse proxies Compile your JavaScript. There is Closure compiler library to do it as well (sure there are others, just search for 'JavaScript compiler' too) Use CDNs (Content Delivery Network) - especially for your large media files and so on. Consider different types of storage for your data, for example, files, key/value stores, etc. - not only SQL Server Last but not least, test your web site for performance