Layout View In ASP.NET Core 6 | _Layout.cshtml ASP.NET | Master Page ASP.Net Core
using Microsoft.AspNetCore.Mvc;
namespace ControllersAndActions.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View(); //ViewResult, PartialViewResult, JsonResult etc
}
public IActionResult About()
{
return View();
}
public IActionResult Contact()
{
return View();
}
public string Display()
{
return "Welcome To Programentor";
}
public int DisplayId(int id)
{
return id;
}
}
}
Above File is ControllersAndActions\Controllers\HomeController.cs File
@{
ViewData["Title"] = "About";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>About Page</h1>
<p>This is my About Us Page</p>
Above File is ControllersAndActions\Views\Home\About.cshtml
@{
ViewData["Title"] = "Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Contact Page</h1>
<p>This is my Contact Us Page</p>
Above File is ControllersAndActions\Views\Home\Contact.cshtml
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
<h4>To comment multiple code, use Ctrl + k then Ctrl + C</h4>
<p>This is my Home Page</p>
@* <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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<header style="background-color:yellow;">
<h1>Header Portion<h1/>
</header>
@RenderBody()
<footer style="background-color:aqua;">
<h1>Footer Portion</h1>
</footer>
</body>
</html>
Above File is ControllersAndActions\Views\Shared\_Layout.cshtml
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
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment