12

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

Lets say I have a view and I want to add some c# code to it something like Datetime.Now.ToString();
Now typically I could add this line of code to my cshtml file, save the file, refresh the page and see the result without compiling.

If I would do the same "by the book" by adding a property to my model, assigning the Datetime.Now.ToString() in my controller and simply rendering the new var I would need to compile my code in order to see the changes.

How does this magic work? If it's so simple, why can't this be done with .cs files as well?

P.s. the same question is relevant for asp.net applications and aspx\ascx pages.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99

2 Answers2

14

.cshtml files will be compiled just-in-time, that is when a request arrives regarding those pages.

However controllers are pre-compiled and stored into your project's DLL files.

Deciding which one to use, depends on your needs. Pre-compiling gives you less response time (because you've compiled the code before) but just-in-time compiling offers you flexibility.

Alireza
  • 4,976
  • 1
  • 23
  • 36
  • I hate when i change single character of a view and it takes 2 minutes to load the project.!!!! unbearable. make me wanna switch to php. – DarthVader Jul 26 '14 at 07:40
  • Interesting..From what I know: cs code is compiled into IL code(dll files) and the JIT compiler compiles the code "on the fly" with optimizations to machine code. Is cshtml code compiled before to IL code? – Amir Popovich Jul 26 '14 at 07:41
  • 2
    @AmirPopovich I used the term "just-in-time" here by the general meaning. It is compiled to IL of course, but only when needed (per request). If performance is important to you (which most of time, it is) you should avoid writing code inside your cshtml – Alireza Jul 26 '14 at 08:28
  • 2
    @DarthVader You will get more better results if you keep the cshtml clean and move your codes into the controller. May the force be with you! – Alireza Jul 26 '14 at 08:36
  • 1
    @Alireza - +1 & Accepted - Thank you for your answers. Wiktor's answer is very good as well but you answered first. – Amir Popovich Jul 26 '14 at 09:56
  • I even liked @Wiktor Zychla's answer better than mine, because he remembered to mention `caching` of the compiled scripts. – Alireza Jul 26 '14 at 21:53
9

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

There is no magic, the runtime decides when to run the compiler (e.g. when the resource has been changed since last run) and then invokes the compiler to create an imperative code out of the declarative code (e.g. an *.aspx is compiled into *.cs). Then it invokes regular language compiler to get a *.dll containing CIL.

This takes some time for the first time a resource is accessed, it could be handy to precompile all declarative resources ahead of time.

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

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • +1 - Thanks for your answer. It is a very good answer. Alireza's answer was good as well and I've accepted his since he answered first. – Amir Popovich Jul 26 '14 at 09:57