Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC5 each Razor Page very slow on first load

This is not the same delay experiences when the first request arrives, but this is a delay that is experienced each time a Razor based view is accessed for the first time, it can take a second or two. All subsequent requests to that view are very fast. This happens even for simple views that are not doing any kind of programmatic work (such as accessing a database etc).

I've already ensured that debug=false in the compilation tag under system.web in the config file.

I've also removed set Razor as the only view-engine via the Global.asax

What could be causing this delay? This seems like a problem experienced in the old asp.net 'website' days before it moved to a 'web application' where each .aspx.cs codebehind was compiled at deployment rather than at runtime. Does Razor still suffer from this?

like image 226
simbolo Avatar asked Jul 29 '14 01:07

simbolo


People also ask

Why are ASP Net Applications slightly slower on first load?

Typically an application will always take a little extra time to load as the application domain starts up. Things helping exacerbate this could be anything from poorly written code (IE: Application_Start) to certain libraries you may be using (ORMs for example). How many modules do you have loaded?

Is razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder.

Should I learn razor pages or MVC?

MVC is good for those web application which has lots of dynamic server pages, REST APIs and AJAX call. For basic web pages and simple user input forms, we should go for Razor pages. Razor pages are similar to ASP.Net web forms in the context of structure, except page extension.


1 Answers

The issue is caused by the parsing and compilation of the Razor views. Once views are compiled, they execute very quickly. Views are only parsed and compiled on the first request for the view, or if the view has been modified since the last compile.

You can resolve this on a deployed WebApp by precompling your views as a part of your Publish process. See the image below on how to do it in VS2012, using the standard publish dialog.

You can select the updatable option if you wish, but for a production site I wouldn't recommend it.

enter image description here

like image 86
Xenolightning Avatar answered Sep 22 '22 00:09

Xenolightning