HomeController.cs
(Cont.)
public class HomeController: Controller { private readonly ILogger<HomeController> _logger; public HomeController( ILogger<HomeController> logger ) { _logger = logger; } |
HomeController: Controller
HomeController
inherits the Controller
class.
ILogger<TCategoryName>
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
return View( );
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 } ); } |
[ ]
[ ]
(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
Activity.Current?.Id ?? HttpContext.TraceIdentifier
?.
) is used to safely access members or methods of an object that might be null, and
??
) provides a fallback value if the left-hand side is null.
|
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 |