Difference between ‘@’ and ‘@{}’

Do you know friends their is a difference between writing @{ Html.AntiforgeryToken();} and @Html.AntiforgeryToken.

The difference is :-

When we writing @Html.AntiforgeryToken() it means it will write the output of that method inside the page. So remember always when we writing @ symbol before any method at that time the ‘@’ behaves differently that it will call response.write() method internally and print the the output inside the page.
Where as @{//Inside any method} in that also we call the method same way but the difference is that it will not do any thing with output.

So here if we write @Html.AntiforgeryToken like this then it will create a hidden field inside the page with the name of Antiforgery token.

But if we do @{ Html.AntiforgeryToken();} then it will not print any thing inside the page.

Note :‘@’ means Response.write Encoded internally.

Example :- If you try the above method i.e Html.AntiforgeryToken().
Then writing this way @{Html.AntiforgeryToken();} gives you run time exception; because inside your page it won’t create any hidden field for AntiforgeryToken.
But when we write @Html.AntiforgeryToken(); then it work fine.

Key Capsule :- If any where if you see inside @{} block we are again writing @ symbol and calling the method then it mean that we want to print some html content.

For eg:-

FWebControls.FOption ctrlMarginAccount = new FWebControls.FOption();
ctrlMarginAccount.CreateControl(sId: "optMarginAccount", sValue: "", sURL: MyHelper.GetOptionControlURL(), sWidth: "", sClassName: "Ftxtbox", iMasterTypeId: 1, bMandatory: true, bExactMatch: true);
@ctrlMarginAccount.GetHtml(); //This line if you see carefully i used @ symbol which mean that i want to print some thing inside the page.


Explanation :- If i write “ctrlMarginAccount.GetHtml()” then also the method called but it won’t print any thing inside the page. But when we add the ‘@’ before it then it called internally response.write method and print all the html content inside the page.

So all above Key thing to remember that calling @ means it will print any thing inside the page.


Reply your Feedback.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s