ASP.NET Core
Overview
ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, and internet-connected applications. With ASP.NET Core, you can build web apps, IoT apps, and mobile backends.
Getting Started
To start with ASP.NET Core, you need to have the following prerequisites:
- Install the .NET SDK from the official website.
- An integrated development environment (IDE) like Visual Studio or Visual Studio Code.
Creating a New ASP.NET Core Project
You can create a new ASP.NET Core project using the .NET CLI or Visual Studio. Here, we'll use the .NET CLI:
dotnet new webapp -o MyWebApp
cd MyWebApp
dotnet run
This will create a new web application and run it. You can access it at http://localhost:5000
.
Project Structure
The ASP.NET Core project structure includes several important files and folders:
Program.cs
: The main entry point of the application.Startup.cs
: Configures services and the app's request pipeline.appsettings.json
: Configuration settings for the application.Controllers
folder: Contains controller classes responsible for handling incoming HTTP requests.Views
folder: Contains the Razor view files used for rendering HTML.
Creating a Controller
Controllers in ASP.NET Core handle incoming HTTP requests and produce responses. Here's an example of a simple controller:
using Microsoft.AspNetCore.Mvc;
namespace MyWebApp.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
Creating a View
Views in ASP.NET Core are used to render HTML. They are typically written using Razor syntax. Here's an example of a simple view:
@{
ViewData["Title"] = "Home Page";
}
<div class="swf-lsn-text-center">
<h1 class="swf-lsn-display-4">Welcome</h1>
<p>Welcome to your new ASP.NET Core application.</p>
</div>
Dependency Injection
ASP.NET Core has built-in support for dependency injection (DI), which makes it easy to manage and inject dependencies. To add a service to the DI container, use the ConfigureServices
method in Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddTransient<IMyService, MyService>();
}
Middleware
Middleware is software that's assembled into an app's pipeline to handle requests and responses. Each component in the pipeline can either pass the request to the next middleware or handle it directly. Here's how you can add middleware to your application:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Configuration
ASP.NET Core provides a unified way to configure an application using configuration providers. You can store configuration settings in various places like appsettings.json
, environment variables, or command-line arguments. Here's an example of how to read configuration settings:
public class MyService
{
private readonly IConfiguration _configuration;
public MyService(IConfiguration configuration)
{
_configuration = configuration;
}
public string GetSetting()
{
return _configuration["MySetting"];
}
}
Logging
ASP.NET Core includes a flexible logging framework that makes it easy to add logging to your application. Here's how you can configure and use logging:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(config =>
{
config.AddConsole();
config.AddDebug();
});
services.AddControllersWithViews();
}
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index page is accessed.");
return View();
}
}
Conclusion
ASP.NET Core is a powerful framework for building modern web applications. By following this tutorial, you have learned the basics of setting up an ASP.NET Core project, creating controllers and views, using dependency injection, configuring middleware, handling configuration settings, and adding logging to your application.