Razor in ASP.NET Core | Razor Syntax | Razor View Engine | Views in ASP.NET Core

 using Microsoft.AspNetCore.Mvc;


namespace ControllersAndActions.Controllers

{

    public class HomeController : Controller

    {

        public IActionResult Index()

        {

            return View();  //ViewResult, PartialViewResult, JsonResult etc

        }

        public string Display()

        {

            return "Welcome To Programentor";

        }

        public int DisplayId(int id)

        {

            return id;

        }

    }

}

Above File is ControllersAndActions\Controllers\HomeController.cs File






namespace ControllersAndActions
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Services.AddControllersWithViews();
            var app = builder.Build();

            app.MapControllerRoute(
                name:"default",
                pattern:"{controller=Home}/{action=Index}/{id?}"
                );

            //app.MapGet("/", () => "Hello World!");

            app.Run();
        }
    }
}
Above File is ControllersAndActions\Program.cs File








@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>
<h2>C# server side code will convert into HTML code using Razor view engine</h2>
<h3>DateTime is a class of C# and ".Now" is its property</h3>
<h1>Current Date and Time is @DateTime.Now</h1>
<h1>Current Year is @DateTime.Now.Year</h1>

@{
    var name = "Programentor";
    var age = 30;
}
<h2>Name = @name</h2>
<h2>Age = @age</h2>

@{
    var MyName = "Adil";
    if (MyName.Equals("Adil"))
    {
        <h3>Hey Adil, How are you ?</h3>
    }
    else
    {
        <h3>By</h3>
    }
}

@{
    for (int i=0; i<10; i++)
    {
        <h2>@i</h2>
    }
}

@{
    string[] names = { "Adil","Kumar","Prem"};
    foreach (var item in names)
    {
        <h3>@item</h3>
    }
}
Above File is ControllersAndActions\Views\Home\Index.cshtml














Comments

Popular posts from this blog

Map, MapGet, MapPost, MapPut and MapDelete methods in ASP.NET Core MVC - Routing ASP.NET

Layout View In ASP.NET Core 6 | _Layout.cshtml ASP.NET | Master Page ASP.Net Core

Attribute Routing in ASP.NET Core MVC