How does Microsoft Razor Syntax run server side code without exposing it to the client?

I am having trouble wrapping my head around how Razor syntax works.

It is my understanding that it will allow you to embed server-side code directly into the page, but it keeps all of that from the client even though the client can interact with it.

I'm used to ASP.net web views and things of that nature - does the Razor syntax essentially do postbacks and all of the code is contained within the page, or is the way that the client interacts with the server-side code fundamentally different?

If this is too open-ended, please leave me comments as to how to focus the question more. I fear I don't yet understand enough to make finely-focused questions.

Thanks!

Upvotes: 2

Views: 6778

Answers (4)

Triet Doan
Triet Doan

Reputation: 12085

From my understanding:

1. How Razor syntax works:

You can see the Razor is similar to the scriplet in Web Form. For example, you want to get the Name of the Human class, in .cshtml file, you write:

<div>Name is @Model.Name</div>

And in aspx, you write:

<div>Name is <%= Human.Name %></div>

Both Razor and scriplet will be processed before returning the HTML files to client. Follow the above example, if you view the source of your HTML web page, you will see (Forte is just an example):

<div>Name is Forte</div>

Because the code was processed, so, what the client see is the HTML result only.

2.How do clients interact with server?

In Web Form, when you want to go to server (like handle click button event), you can generate the function to call by set OnClickListener for the button, and everything is done automatically for you.

However, in MVC.NET, you have to do it through Ajax call, or put your button in a <form>.

If you have any question, feel free to let me know :)

Upvotes: 3

MartinDotNet
MartinDotNet

Reputation: 2505

Essentially it compiles the view like any other csharp/vb.net file. However, it does it on-the-fly (the first time you hit it, and the underlying file hasn't changed).

If you want to see what the generated code looks like, take a look at https://github.com/RazorGenerator/RazorGenerator This allows you to actually generate the same code as part of MSbuild and deploy it.

Upvotes: 0

e4rthdog
e4rthdog

Reputation: 5223

Lets say you have this in a view

@{ var var1 =   "Hello World"; }
<p>The value of var1 is: @var1</p> 

Everything happens on the server

When a user is requesting this page the server gets the text above and sends it to the view engine.

Then the server runs the text line by line and "compiles" it.

Then the server snds the result back to the user:

The value of var1 is: Hello World

If the user makes a view source he will see only:

<p>The value of myMessage is: Hello World</p>

So the user on the client writes a simple URL , and the result he is getting is ONLY the line above.

Upvotes: 1

Clay Sills
Clay Sills

Reputation: 235

mvc runs all that stuff server side and provides the client with the resulting html/javascript generation.

Upvotes: 3

Related Questions