Download Logo

Services in Razor views

Accessing Services in Views 1. Using Dependency Injection in Views with Razor Pages To use services directly in Razor Pages, follow these steps: 1.1. Register Services in Startup.cs Ensure your service is registered in the DI container within Startup.cs (or Program.cs in .NET 6 and later). 1 2 3 4 5 public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddScoped<IMyService, MyService>(); // Register your service } 1.2. Inject Services into the View In Razor Pages (.cshtml files), you can inject services directly into the page using the @inject directive. ...

August 3, 2024 · 3 min · 530 words · PrashantUnity
Download Logo

Singleton in C#

Basic Implementation 1 2 3 4 5 6 7 8 9 10 Singleton singleton = Singleton.Instance; sealed class Singleton { public static Singleton Instance { get; } = new(); private Singleton() { } } Null Checking 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Singleton singleton = Singleton.Instance; sealed class Singleton { private static Singleton _instance = default!; public static Singleton Instance { get { if (_instance is null) { _instance = new Singleton(); } return _instance; } } private Singleton() { Console.WriteLine("Instantiating singleton"); } } Thread Safe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Singleton singleton = Singleton.Instance; sealed class Singleton { private static Singleton _instance = default!; private static object _lock = new(); public static Singleton Instance { get { if (_instance is null) { Console.WriteLine("Locking"); lock (_lock) { if (_instance is null) { _instance = new Singleton(); } } } return _instance; } } private Singleton() { Console.WriteLine("Instantiating singleton"); } } using Dotnet Base Class Library (BCL) 1 2 3 4 5 6 7 8 9 10 11 12 13 Singleton singleton = Singleton.Instance; sealed class Singleton { private static readonly Lazy<Singleton> _lazyInstance = new(() => new()); public static Singleton Instance => _lazyInstance.Value; private Singleton() { Console.WriteLine("Instantiating singleton"); } } Jon Skeet way 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Singleton singleton = Singleton.Instance; sealed class Singleton { public static string ClassName; public static Singleton Instance => Nested.Instance; private class Nested { internal static Singleton Instance { get; } = new(); static Nested() { } } private Singleton() { } static Singleton() { ClassName = "asdf"; } } Using Microsoft Dependency Injection Package 1 2 3 <ItemGroup> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> </ItemGroup> 1 2 3 4 5 6 7 8 9 10 11 12 13 using Microsoft.Extensions.DependencyInjection; IServiceCollection services = new ServiceCollection(); services.AddSingleton<Singleton>(); var serviceProvider = services.BuildServiceProvider(); var instance = serviceProvider.GetRequiredService<Singleton>(); class Singleton() { }

August 3, 2024 · 2 min · 371 words · PrashantUnity
Download Logo

ViewBag & friends

Data Passing Between Controller and View Using Form Like POST Method ViewBag.Number and (int)ViewData[“Number”] from controller to View Controller to Controller TempData 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public ActionResult SubmitForm(MyModel model) { if (ModelState.IsValid) { // Process the form data // Store a success message in TempData TempData["Message"] = "Form submitted successfully!"; // Redirect to another action return RedirectToAction("Confirmation"); } return View(model); } public ActionResult Confirmation() { // Retrieve the message from TempData ViewBag.Message = TempData["Message"]; return View(); } 1. Using ViewData ViewData is a dictionary-based way to pass data to the view. It uses the ViewDataDictionary class and is accessed by string keys. ...

August 3, 2024 · 2 min · 374 words · PrashantUnity

Question sets