Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't a compilation needed when updating cshtml files with .net code?

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.

like image 392
Amir Popovich Avatar asked Jul 26 '14 07:07

Amir Popovich


People also ask

Do Cshtml files need to be compiled?

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.

What are Cshtml files used for?

What is a CSHTML file? A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser.


1 Answers

.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.

like image 83
Alireza Avatar answered Oct 12 '22 14:10

Alireza