Asp.net Razor代码到哪里去了?

Asp.net Razor代码到哪里去了?,asp.net,html,razor,asp.net-webpages,Asp.net,Html,Razor,Asp.net Webpages,当我创建一个Web表单项目时,我的代码被编译成一个DLL,由IIS服务器处理。当我使用Javascript时,它由浏览器进行解释,我可以使用诸如Chrome开发者工具之类的工具或通过检查源代码来找到它 但是,当我使用Razor语法创建ASP.NET网页时,我在任何地方都找不到代码。因为它不需要编译,所以它没有被放入DLL中,我也无法使用Chrome的inspect工具找到它的任何痕迹 那么,Razor代码去哪里了呢?它们确实是在运行时编译的。您可以在已编译web表单所在的位置找到生成的代码文件和

当我创建一个Web表单项目时,我的代码被编译成一个DLL,由IIS服务器处理。当我使用Javascript时,它由浏览器进行解释,我可以使用诸如Chrome开发者工具之类的工具或通过检查源代码来找到它

但是,当我使用Razor语法创建ASP.NET网页时,我在任何地方都找不到代码。因为它不需要编译,所以它没有被放入DLL中,我也无法使用Chrome的inspect工具找到它的任何痕迹


那么,Razor代码去哪里了呢?

它们确实是在运行时编译的。您可以在已编译web表单所在的位置找到生成的代码文件和临时DLL:

C:\Windows\Microsoft.NET\Framework64\<version>\Temporary ASP.NET Files\<app>\
C:\Windows\Microsoft.NET\Framework64\\ASP.NET临时文件\。这将增加解决方案的编译时间(以我的经验),但它对于检测在运行时之前不会被注意到的错误非常有用。需要时我会打开它

Razor解析器/生成器代码包含在System.Web.Razor项目/程序集中(作为MVC源代码的一部分提供)。由于最终结果是一个c#类,c#编译器可能会像处理任何其他类一样从那里处理它

为视图生成的代码类似于(摘自我的一个项目中的“重置密码”页面)

命名空间ASP{
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用System.Web;
使用System.Web.Helpers;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.Web网页;
使用System.Web.Mvc;
使用System.Web.Mvc.Ajax;
使用System.Web.Mvc.Html;
使用System.Web.Routing;
公共类\页面\区域\匿名\视图\主页\重置密码\ cshtml:System.Web.Mvc.WebViewPage{
#隐藏线
公共(页面)区域(匿名)视图(主页)重置密码(cshtml){
}
受保护的ASP.global\U asax应用程序实例{
得到{
返回((ASP.global_asax)(Context.ApplicationInstance));
}
}
公共覆盖无效执行(){
const string title=“重置密码”;
ViewBag.Title=标题;
BeginContext(“~/Areas/Anonymous/Views/Home/ResetPassword.cshtml”,302,63,true);
Writeleteral(“\r\n\r\n\r\n\r\n\r\n\r\n\r\n”);
#行默认值
#隐藏线
使用(@Html.BeginForm(“ResetPassword”,“Home”,FormMethod.Post,new{id=“main form”}))
{
编写(Html.ValidationSummary());
//即使是简单的视图也会产生一个大的代码文件

无论您使用Razor、传统代码隐藏(单独的文件)还是带有内嵌HTML和C#的.aspx,或多或少都会发生相同的事情。所有这些都会转换为服务器端代码,并在服务器端执行,发出HTML(以及在源代码页中从未见过的注入javascript)在运行时。当服务器端代码生成要发送到客户端的页面时,源页面中的HTML将成为服务器端代码使用的文本块

服务器通常会编译您的页面,并运行编译后的代码;看起来“解释”的内容实际上是在第一次页面加载时及时编译的,并缓存在内存中,以便后续页面加载

如果您在站点的任何位置都找不到页面已编译输出的证据,可能是因为该页面只是及时编译的,从未实际写入dll。

实际上,dll是JIT编译过程的结果。内存中没有缓存任何内容。
namespace ASP {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Web.Mvc.Html;
    using System.Web.Routing;

    public class _Page_Areas_Anonymous_Views_Home_ResetPassword_cshtml : System.Web.Mvc.WebViewPage<Web.UI.Areas.Anonymous.ResetPasswordViewModel> {

#line hidden

        public _Page_Areas_Anonymous_Views_Home_ResetPassword_cshtml() {
        }

        protected ASP.global_asax ApplicationInstance {
            get {
                return ((ASP.global_asax)(Context.ApplicationInstance));
            }
        }

        public override void Execute() {

    const string title = "Reset Password";
    ViewBag.Title = title;


BeginContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 302, 63, true);

WriteLiteral("</h1>\r\n            </div>\r\n        </div>\r\n    </div>\r\n    <div");

EndContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 302, 63, true);

BeginContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 365, 12, true);

WriteLiteral(" class=\"two\"");

EndContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 365, 12, true);

BeginContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 377, 15, true);

WriteLiteral(">\r\n        <div");

EndContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 377, 15, true);

BeginContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 392, 19, true);

WriteLiteral(" class=\"banner-box\"");

EndContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 392, 19, true);

BeginContext("~/Areas/Anonymous/Views/Home/ResetPassword.cshtml", 411, 5, true);

WriteLiteral(">\r\n\r\n");

            #line default
            #line hidden

             using( @Html.BeginForm( "ResetPassword", "Home", FormMethod.Post, new { id = "main-form" } ) )
            {

                Write(Html.ValidationSummary());

// etc. etc. Even simple views result in a large code file