Jessica
Jessica

Reputation: 2051

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

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?

Upvotes: 1

Views: 652

Answers (1)

Stevo
Stevo

Reputation: 1434

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>");
    }
}

Upvotes: 2

Related Questions