dotnet (1)
dotnet (1)
dotnet (1)
202203103510495
Practical No. 6
CODE
EmployeeContoller.cs using
Microsoft.AspNetCore.Mvc; using
EmployeeManagement.Models; using
System.Collections.Generic;
namespace EmployeeManagement.Controllers
{ public class EmployeesController : Controller
{ public IActionResult Index()
{
// Sample data for demonstration purposes
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "John Doe", Position = "Manager" },
new Employee { Id = 2, Name = "Jane Smith", Position = "Clerk" }
};
ManagerController.cs using
Microsoft.AspNetCore.Mvc; using
EmployeeManagement.Models; using
System.Collections.Generic; namespace
Enrolment no. 202203103510495
EmployeeManagement.Controllers { public
class ManagersController : Controller
{ public IActionResult Index()
{
// Sample data for demonstration
var managers = new
List<Manager>
{
new Manager { Id = 1, Name = "Alice Johnson", Position = "Manager",
NumberOfTeams = 3 }, new Manager { Id = 2, Name = "Bob Brown", Position
= "Manager", NumberOfTeams = 5 }
};
return View(managers);
}
}
}
clerkContoller.cs using
Microsoft.AspNetCore.Mvc; using
EmployeeManagement.Models; using
System.Collections.Generic;
namespace EmployeeManagement.Controllers
{ public class ClerksController : Controller
{ public IActionResult Index()
{
// Sample data for demonstration
var clerks = new List<Clerk>
{
new Clerk { Id = 1, Name = "Charlie Smith", Position = "Clerk", Department =
"Finance" }, new Clerk { Id = 2, Name = "Dana White", Position = "Clerk",
Department = "HR" }
};
return View(clerks);
}
}
}
Enrolment no. 202203103510495
Models/Employee.cs namespace
EmployeeManagement.Models
{ public class Employee
{ public int Id { get; set; } public string? Name { get; set; } // Nullable
string type for Name public string? Position { get; set; } // Nullable
string type for Position
}
}
Models/Manager.cs namespace
EmployeeManagement.Models
{
public class Manager : Employee
{
public int? NumberOfTeams { get; set; } // Nullable int type for NumberOfTeams
}
}
Models/clerk.cs namespace
EmployeeManagement.Models
{
public class Clerk : Employee
{ public string? Department { get; set; } // Nullable string type for Department
}
}
Views/clerk/Index.cshtml
@model IEnumerable<EmployeeManagement.Models.Clerk>
<h2>Clerks List</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Department</th>
</tr>
</thead>
<tbody>
Enrolment no. 202203103510495
Views/Manager/Index.cshtml
@model IEnumerable<EmployeeManagement.Models.Manager>
<h2>Managers List</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Number of Teams</th>
</tr>
</thead>
<tbody>
@foreach (var manager in Model)
{
<tr>
<td>@manager.Id</td>
<td>@manager.Name</td>
<td>@manager.Position</td>
<td>@manager.NumberOfTeams</td>
</tr>
}
</tbody>
</table>
Views/Employee/Index.cshtml
@model IEnumerable<EmployeeManagement.Models.Employee>
Enrolment no. 202203103510495
<h2>Employees List</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody>
@foreach (var employee in Model)
{
<tr>
<td>@employee.Id</td>
<td>@employee.Name</td>
<td>@employee.Position</td>
</tr>
}
</tbody>
</table>
Data/Applicationdbcontext.cs
using Microsoft.EntityFrameworkCore;
using EmployeeManagement.Models;
namespace EmployeeManagement.Data
{ public class ApplicationDbContext : DbContext
{ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
base(options)
{
}
OUTPUT
Enrolment no. 202203103510495
Enrolment no. 202203103510495
Practical No. 8
CODE Program.cs
using WebDownloader.Services; var builder =
WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient<IWebDownloadService, WebDownloadService>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default", pattern:
"{controller=Home}/{action=Index}/{id?}");
app.Run();
Enrolment no. 202203103510495
Services/WebDownloadService.cs
using System.Net.Http; using
System.Threading.Tasks;
namespace WebDownloader.Services
{ public interface IWebDownloadService
{
Task<string> DownloadContentAsync(string url);
}
@{
ViewData["Title"] = "Home Page";
}
<h1>Download Content</h1>
OUTPUT
Enrolment no. 202203103510495
Practical No. 9
CODE
HomeController.cs using
Microsoft.AspNetCore.Mvc; using
Microsoft.AspNetCore.Http; using
System;
namespace CookieSessionDemo.Controllers
{ public class HomeController : Controller
{ public IActionResult Index()
{
// Set a cookie
Response.Cookies.Append("UserName", "JohnDoe", new CookieOptions { Expires
= DateTimeOffset.UtcNow.AddDays(1) });
return View();
}
return View();
}
public IActionResult QueryStringDemo(string name)
Enrolment no. 202203103510495
{
ViewBag.Name = name;
return View();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization(); app.UseSession();
// Add this line to use session
app.MapControllerRoute(
Enrolment no. 202203103510495
app.Run();
Views/Home/Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps
with ASP.NET Core</a>.</p>
</div>
@* Index.cshtml *@
<h1>Index Page</h1>
<p>Welcome to the Cookie and Session demo!</p>
<a href="/Home/About">Go to About Page</a>
<br />
<a href="/Home/QueryStringDemo?name=JohnDoe">Pass Query String</a>
<br />
<form method="post" action="/Home/HiddenFieldDemo">
<input type="hidden" name="hiddenFieldValue" value="HiddenValue" />
<button type="submit">Submit Hidden Field</button>
</form>
View/Home/About.cshtml
@* About.cshtml *@
<h1>About Page</h1>
<p>Cookie Value: @ViewBag.UserName</p>
<p>Session Value: @ViewBag.SessionValue</p>
<a href="/">Back to Index</a>
View/Home/QueryStringDemo.cshtml
@* QueryStringDemo.cshtml *@
<h1>Query String Demo Page</h1>
<p>Query String Name: @ViewBag.Name</p>
Enrolment no. 202203103510495
OUTPUT
Enrolment no. 202203103510495
Enrolment no. 202203103510495
Practical No. 10
CODE
HomeController.cs using Microsoft.AspNetCore.Mvc; using
Microsoft.Extensions.Caching.Memory; // Import necessary namespace
_memoryCache.Set("CachedTime", cachedTime,
cacheEntryOptions);
}
ViewBag.CachedTime = cachedTime;
return View();
}
}
}
Program.cs
using Microsoft.Extensions.Caching.Memory; var
builder = WebApplication.CreateBuilder(args);
app.UseHttpsRedirection(); app.UseStaticFiles();
app.UseRouting(); app.UseAuthorization();
app.MapControllerRoute(
name: "default", pattern:
"{controller=Home}/{action=Index}/{id?}");
app.Run();
Views/Home/Index.html
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Current Time: @ViewBag.Timestamp</p>
<p>Cached Time: @ViewBag.CachedTime</p>
</div>
OUTPUT