How does C# run inside a Razor view without compilation?

夙愿已清 提交于 2019-12-08 03:17:10

问题


I couldn't seem to find an answer to this on the web.

Since C# is a compiled language, how is code inside a Razor view able to execute without the developer having to build again?

For example, if I load a page with this code: @{string test = "123";}.

But then change the code to this: @{string test = "test";}

How do I not need to rebuild? Also, is the thing that is allowing this to happen a C# feature or an MVC feature?


回答1:


The razor engine basically "compiles" the view. Something like this...

@model Person
<html>
  <p>
  @Model.FirstName
  </p>
</html>

is actually compiled into a method in a class. I can't remember the exact functions it calls but it would look something along the lines of this:

class RazorPage<T>
{
    T Model;

    void Page
    {
        Write("<html>");
        Write("  <p>");
        Write(Model.FirstName);
        Write("  </p>");
        Write("</html>");
    }
}


来源:https://stackoverflow.com/questions/51190194/how-does-c-sharp-run-inside-a-razor-view-without-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!