Using MVC to Develop the Project (Cont.)


MVC Controllers: HomeController.cs (Cont.)
 public class HomeController: Controller {
   private readonly ILogger<HomeController> _logger;
   public HomeController( ILogger<HomeController> logger ) { _logger = logger; }

HomeController: Controller
It means that the HomeController inherits the Controller class.

ILogger<TCategoryName>
It is a generic interface for logging where the category name is derived from the specified TCategoryName type name. Generally used to enable activation of a named ILogger from dependency injection.

 public IActionResult Index( )   { return View( ); }
 public IActionResult Privacy( ) { return View( ); }

IActionResult
The interface defines a contract that represents the result of an action method.

return View( );
When you use return View(); without any parameters, ASP.NET MVC automatically looks for a view file that matches the name of the action method in the corresponding controller’s folder within the Views directory.

 [ ResponseCache( Duration = 0,
     Location = ResponseCacheLocation.None, NoStore = true ) ]
 public IActionResult Error( ) {
   return View( new ErrorViewModel { RequestId =
     Activity.Current?.Id ?? HttpContext.TraceIdentifier } );
 }

[ ]
The [ ] (square bracket) notation is primarily used for Data Annotations. These are attributes applied to properties or classes in your model to define validation rules, display metadata, or specify how data should be handled.

ResponseCache
Response caching in ASP.NET Core reduces the number of requests a client or proxy makes to a web server and decreases the workload on the server by storing responses.

Activity.Current?.Id ?? HttpContext.TraceIdentifier

  • Null-conditional operator (?.) is used to safely access members or methods of an object that might be null, and
  • Null-coalescing operator (??) provides a fallback value if the left-hand side is null.

C:\ASP.NET-workspace\HelloWorld\Controllers\HomeController.cs
 using System.Diagnostics;
 using HelloWorld.Models;
 using Microsoft.AspNetCore.Mvc;

 namespace HelloWorld.Controllers {
  public class HomeController: Controller {
   private readonly ILogger<HomeController> _logger;

   public HomeController( ILogger<HomeController> logger ) { _logger = logger; }

   public IActionResult Index( ) { return View( ); }

   public IActionResult Privacy( ) { return View( ); }

   [ ResponseCache( Duration = 0,
     Location = ResponseCacheLocation.None, NoStore = true ) ]
   public IActionResult Error( ) {
    return View( new ErrorViewModel { RequestId =
     Activity.Current?.Id ?? HttpContext.TraceIdentifier } );
   }
  }
 }




      “I travel a lot. TSA looks at my name,    
      and suddenly I’m the most interesting man in the world.    
      ‘Mr. Mohammed...please step this way.”    
      — Mo Amer