C# ASP.NET MVC 呈现似乎很慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16281366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:37:08  来源:igfitidea点击:

ASP.NET MVC rendering seems slow

c#asp.net-mvcperformanceasp.net-mvc-4

提问by Jez

I've created a brand new MVC4 web application in Visual Studio, and done nothing more with it than add a Home controller and a "Hello world" index view for it. I then installed the MiniProfiler NuGet package and put the necessary couple of lines in _Layout.cshtml. This is what I get when I run the site in Release mode (hosted in IIS):

我在 Visual Studio 中创建了一个全新的 MVC4 Web 应用程序,除了为它添加一个 Home 控制器和一个“Hello world”索引视图之外,什么也没做。然后我安装了 MiniProfiler NuGet 包并将必要的几行放在_Layout.cshtml. 这是我在发布模式下运行站点时得到的结果(托管在 IIS 中):

MVC rendering picture

MVC渲染图

The rendering time varies by pageload, but 130ms is about as fast as it gets. This seems a bit slow to me, as I've seen other people who get pages rendered in 30ms or quicker. Any ideas why the rendering would be this slow with a brand new empty MVC4 project? My processor is an Intel Core i5-2400 and the machine has 16GB RAM.

渲染时间因页面加载而异,但 130 毫秒几乎与它一样快。这对我来说似乎有点慢,因为我看到其他人在 30 毫秒或更短的时间内呈现页面。任何想法为什么使用全新的空 MVC4 项目渲染会这么慢?我的处理器是 Intel Core i5-2400,机器有 16GB RAM。

By the way, this is notthe first time the page is loaded; I reloaded the page a few times before getting this 130ms result.

顺便说一下,这不是第一次加载页面;在得到这个 130 毫秒的结果之前,我重新加载了页面几次。

UPDATE:
I followed the advice in the answer from PSCoder (remove all but the RazorViewEngine), and it halved the rendering time:

更新:
我遵循了 PSCoder 的回答中的建议(删除除 RazorViewEngine 之外的所有内容),并将渲染时间减半:

MVC rendering picture 2

MVC渲染图2

This is really good, but I still get about 70ms or higher for the main Renderaction of the page; ideally I'd like to halve that or better.

这真的很好,但我仍然得到大约 70 毫秒或更高Render的页面主要操作;理想情况下,我想将其减半或更好。

Specifically, I'd like to ask:

具体我想问一下:

  • Does this rendering time seem overly slow or is it average for my machine?
  • Is there any way I can speed it up?
  • 这个渲染时间是不是看起来太慢了,还是我的机器是平均的?
  • 有什么办法可以加快速度吗?

采纳答案by PSL

This could help improve ASP.NET MVC related performance issue , one performance improvement that you can do is to clear all the view engines and add the one(s) that you use. say for ex:- RazorViewEngine. MVC registers 2 view engines by default Webformsand Razorview engines, so clearing and adding the ones that is used alone will improve the look up performance.

这可以帮助改善与 ASP.NET MVC 相关的性能问题,您可以做的一项性能改进是清除所有视图引擎并添加您使用的视图引擎。例如说:- RazorViewEngine。MVC默认注册了2个视图引擎WebformsRazor视图引擎,所以清除和添加单独使用的会提高查找性能。

You can add this in global.asaxApplication_Start.

您可以在global.asaxApplication_Start.

        ViewEngines.Engines.Clear();    
        ViewEngines.Engines.Add(new RazorViewEngine());      

In order to completely utilize view look up caching and thus again performance gain compile the code in release mode and make sure that your web.configfile is configured with <compilation debug="false" />for view look up caching to kick in.

为了完全利用视图查找缓存,从而再次提高性能,请在发布模式下编译代码,并确保您的web.config文件配置<compilation debug="false" />为启用视图查找缓存。

回答by Stuart.Sklinar

Adding to @PSL 's answer - we only ever check for `.CSHTML files

添加到@PSL 的答案 - 我们只检查 `.CSHTML 文件

ViewEngines.Engines.Clear();

IViewEngine razorEngine = new RazorViewEngine() { FileExtensions = new string[] { "cshtml" } };

ViewEngines.Engines.Add(razorEngine);

Also, make sure you are running in Release Mode- that is absolutely critical, as ASP/Razor/MVC 'applies some pretty aggressive caching' when in release mode

此外,请确保您正在运行Release Mode- 这绝对是至关重要的,因为 ASP/Razor/MVC在发布模式下“应用了一些非常积极的缓存

<compilation targetFramework="4.0" debug="false">in your Web.Configfile.

<compilation targetFramework="4.0" debug="false">在您的Web.Config文件中。

Sam Saffron/Stack Overflow looked into view rendering performance also:

Sam Saffron/Stack Overflow 还研究了渲染性能:

http://samsaffron.com/archive/2011/08/16/Oh+view+where+are+thou+finding+views+in+ASPNET+MVC3+

http://samsaffron.com/archive/2011/08/16/Oh+view+where+are+thou+finding+views+in+ASPNET+MVC3+

回答by Stuart.Sklinar

Views are compiled before use - so on the first occasion they are slow.

视图是在使用前编译的——所以第一次它们很慢。

Subsequently they are recompiled if the .cshtmlfile changes - which means the directories that views are stored in are being monitored. So hard disk speed will be a factor for MVC views.

随后,如果.cshtml文件更改,它们将被重新编译- 这意味着正在监视存储视图的目录。所以硬盘速度将成为 MVC 视图的一个因素。

Even if they do nothing, each rendering engine checks the hard disk for .cshtmlor .aspxfiles. Since removing a rendering engine made it run twice as fast, I suspect disk speed is the problem:

即使他们什么都不做,每个渲染引擎检查硬盘.cshtml.aspx文件。由于删除渲染引擎使其运行速度提高了一倍,我怀疑磁盘速度是问题所在:

  • Views are stored on a network drive, or
  • Hard disk is very slow
  • 视图存储在网络驱动器上,或
  • 硬盘很慢

回答by Kavi

Pre-compile the views to speed up the first-time rendering..

预编译视图以加快首次渲染速度。

Check the below blog..

检查下面的博客..

https://blog.deltacode.be/2017/01/08/fix-slow-startup-of-asp-net-mvc-5-on-azure-app-services/

https://blog.deltacode.be/2017/01/08/fix-slow-startup-of-asp-net-mvc-5-on-azure-app-services/

enter image description here

在此处输入图片说明