ASP.NET Core is a cross-platform, high-performance framework for building modern, cloud-based, internet-connected applications. It is a complete rewrite of the original ASP.NET framework and is designed to be lightweight, modular, and flexible. ASP.NET Core can be used to build web applications, APIs, microservices, and more.
Key Features of ASP.NET Core
- Cross-Platform: ASP.NET Core runs on Windows, macOS, and Linux, allowing developers to build applications that can be deployed on any platform.
- High Performance: It is optimized for performance and can handle a large number of requests efficiently.
- Modular Architecture: ASP.NET Core uses a modular approach, allowing developers to include only the necessary components for their applications.
- Dependency Injection: Built-in support for dependency injection makes it easier to manage dependencies and improve testability.
- Unified Framework: ASP.NET Core unifies the MVC and Web API frameworks, allowing developers to use a single framework for both web applications and APIs.
Getting Started with ASP.NET Core
To create a simple ASP.NET Core application, you need to have the .NET SDK installed. You can create a new project using the command line or Visual Studio. Below is a simple example of a web application created using ASP.NET Core.
Sample Code
// Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
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?}");
});
}
}
Running the Application
After creating the project, you can run the application using the command:
dotnet run
This will start the web server, and you can access your application in a web browser at https://localhost:5001.
Conclusion
ASP.NET Core is a powerful framework for building modern web applications. Its cross-platform capabilities, high performance, and modular architecture make it an excellent choice for developers looking to create scalable and maintainable applications.