Get ready for your next interview with our comprehensive question library
The Startup
class is central to ASP.NET Core applications. It configures:
ConfigureServices
method by adding components (e.g., MVC, EF Core, logging) to the DI container.Configure
method by setting up the request handling pipeline.ConfigureServices
is where you register application services with the dependency injection container. This can include framework services (MVC, Razor Pages, Entity Framework) and custom application services.
Example:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
The Configure
method defines the HTTP request pipeline by adding middleware components. The order in which middleware is added is crucial as it determines how HTTP requests are processed.
Example:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Home/Error");
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Settings are typically configured using:
appsettings.Development.json
.The configuration system supports hierarchical settings and binding to strongly typed classes via the Options pattern.
The appsettings.json
file holds configuration data (like connection strings, logging settings, etc.) in a JSON format. It can be supplemented by environment-specific files and overridden by other configuration sources.
Create separate configuration files named with the environment suffix (e.g., appsettings.Development.json
, appsettings.Production.json
). Set the environment by defining the ASPNETCORE_ENVIRONMENT
variable. The configuration system automatically loads the appropriate file based on the environment.
ASP.NET Core provides a built-in logging API through the ILogger
interface. It supports multiple providers (Console, Debug, EventSource) and can be extended with third-party logging frameworks like Serilog or NLog.
Example:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index page requested");
return View();
}
}
Kestrel is the default, cross-platform web server for ASP.NET Core. It is designed to be lightweight and high performance. In production, it is often used in combination with a reverse proxy like IIS, Nginx, or Apache for additional security and robustness.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime