为什么在使用.net代码更新cshtml文件时不需要编译? [英] Why isn't a compilation needed when updating cshtml files with .net code?

查看:839
本文介绍了为什么在使用.net代码更新cshtml文件时不需要编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Asp.net Mvc,我想知道为什么我不需要编译我的项目时更新.net代码在cshtml文件?
现在如果我们谈论html \css更新,那么我清楚地理解为什么简单的刷新就足够了,但是.net代码在这些情况下如何编译呢?

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?

假设我有一个视图,我想为它添加一些c#代码,例如 Datetime.Now.ToString();

现在通常我可以将这行代码添加到我的cshtml文件,保存文件,刷新页面并查看结果而不编译。

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.

如果我通过向我的模型添加一个属性来执行相同的by the book,分配 Datetime.Now.ToString()在我的控制器,只是渲染新的变量我需要编译我的代码,以查看更改。

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.

这个魔术如何工作?如果这么简单,为什么不能这样做.cs文件?

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

P.s。同样的问题与asp.net应用程序和aspx \ascx页面相关。

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

推荐答案

.cshtml 文件将被及时编译,即当有关这些页面的请求到达时。

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

但是控制器是预先编译并存储到项目的DLL文件中的。

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.

这篇关于为什么在使用.net代码更新cshtml文件时不需要编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆