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.
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 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.
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With