Why is compilation not required when updating cshtml files using .net code?

I am using Asp.net Mvc and I wanted to know why I do not need to compile my project when updating the .net code in cshtml files? Now, if we are talking about html \ css updates, then I clearly understand why a simple update will be enough, but how does .NET code compile on the fly in these cases?

Suppose I have a view and I want to add C # code to it like Datetime.Now.ToString();
Now I usually add this line of code to my cshtml file, save the file, refresh the page and see the result without compilation.

If I did the same thing β€œfrom the book,” adding a property to my model, assigning Datetime.Now.ToString() in my controller and just creating a new var, I would need to compile my code to see the changes.

How does this magic work? If it is so simple, why is it impossible to do with .cs files?

Ps the same question matters for asp.net applications and aspx \ ascx pages.

+8
compiler-construction c # asp.net-mvc
source share
2 answers

.cshtml files will be compiled on time, that is, when the request arrives for these pages.

However, the controllers are precompiled and stored in your project DLL files.

Deciding which one to use depends on your needs. Precompiling gives you less response time (because you already compiled the code), but just-in-time compilation offers you flexibility.

+9
source share

Part of the ASP.NET infrastructure is the ASP.NET compiler. He is responsible for collecting declarative resources (* .aspx, * .ascx, * .cshtml, etc.) into executable code.

There is no magic, the runtime decides when to start the compiler (for example, when the resource has been changed since the last start), and then calls the compiler to create imperative code from the declarative code (for example, * .aspx is compiled into * .cs). He then calls the regular language compiler to get the * .dll containing the CIL.

This takes some time when you first access the resource, so it would be useful to pre-copy all declarative resources in advance.

Overview:

http://msdn.microsoft.com/en-us/library/vstudio/ms178466(v=vs.100).aspx

Precompilation:

http://msdn.microsoft.com/en-us/library/aa983464(v=vs.110).aspx

+7
source share

All Articles