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


当前回答

这不是一个惊天动地的优化,但我想我要把它扔在那里-使用CDN的jQuery,等等。

引用ScottGu自己的话:微软Ajax CDN使你能够显著提高ASP的性能。NET Web Forms和ASP。asp.net MVC应用程序使用ASP。NET AJAX或jQuery。这项服务是免费的,不需要任何注册,可以用于商业和非商业目的。

我们甚至使用CDN为我们的web部件在Moss使用jQuery。

其他回答

除了所有关于在服务器端优化应用程序的重要信息外,我还想说你应该看看YSlow。它是提高客户端站点性能的极好的资源。

这适用于所有网站,而不仅仅是ASP。净MVC。

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:我已经运行了几千次迭代的基准测试,如果有兴趣的话可以在我的博客上发布结果。

当通过LINQ访问数据依赖IQueryable…

为什么使用AsQueryable()而不是List()?

... 并利用一个好的存储库模式:

在存储库模式中加载子记录

这将优化数据访问,以确保只加载需要的数据。

基本的建议是遵循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