Here is what MVC controller looks like:
namespace MyApplication.Controllers
{
public class MyController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
Basically it's job is to do some stuff and then render socalled View which normally represents corresponding web page content. If you don't explicitly specify View name, View helper picks .cshtml file with the same name like the method rendering it. Normally, before rendering, that specific .cshtml file get injected into shared _layout.cshtml file representing a common frame of your site or just a piece of it related to this view in particular. For example, _layout.cshtml could contain header and footer, maybe some sorta menu, I mean, you get the idea. Now you may be wondering why your View files got .cshtml extension, its because they are combination of c# and html so to speak. In order to make it possible, ASP.NET developers invented Razor which serves as a middleman between the two.
This is implicit Razor expression:
@
This is explicit Razor expression:
@()
This is Razor code block:
@{
// C# code only. Not meant to be rendered.
}
Unlike code block, expressions are meant to be evaluated and rendered as html.
All of the above share the same scope, meaning, for example, you can access a variable defined in code block from an expression or another block. By the way, order matters!
No comments:
Post a Comment