CodeBlog.xyz

.Net Core – 50 beginner level questions

September 7, 2023 | by Meir Achildiev

Net Core questions

Here are some beginner level .NET Core questions with their corresponding answers and examples:

  • What is .NET Core?
    .NET Core is a cross-platform, open-source framework for building modern, cloud-based, internet-connected applications. It can be used to build applications for web, mobile, desktop, gaming, IoT, and AI.
  • What is the main difference between .NET Core and .NET Framework?
    .NET Framework is Windows-only, while .NET Core is cross-platform (Windows, Linux, and MacOS). .NET Core is also modular, smaller and faster, which makes it ideal for cloud-based microservices architectures.
  • What is a .NET Core CLI?
    .NET Core CLI is a Cross Platform toolchain for developing, building, running and publishing .NET Core applications.
  • How to create a new .NET Core application using CLI?
    To create a new .NET Core console application, you can use the following command in the CLI: dotnet new console -n MyConsoleApp
  • How to run a .NET Core application?
    Navigate to the project directory and use the following command: dotnet run
  • What is the purpose of the dotnet restore command?
    dotnet restore is used to restore the dependencies and project-specific tools of a project based on your project’s .csproj or .vbproj file.
  • What is the purpose of ‘wwwroot’ folder in .NET Core?
    The ‘wwwroot’ folder in .NET Core is used to store all the static files in your application, including CSS, JavaScript, images, and other files that are not processed by code.
  • What is ASP.NET Core?
    ASP.NET Core is a high-performance, open-source framework for building modern, cloud-based, and internet-connected applications. It’s built on top of .NET Core.
  • What is Middleware in ASP.NET Core?
    Middleware components handle requests and responses, forming a pipeline for request processing in ASP.NET Core. Each piece of middleware can either handle the request and generate a response, or pass the request on to the next piece of middleware.
  • Can you provide a simple example of custom Middleware?
    Here’s an example of custom middleware that logs the execution time for each request:
public class TimingMiddleware
{
    private readonly RequestDelegate _next;

    public TimingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var watch = Stopwatch.StartNew();
        await _next(context);
        watch.Stop();

        var log = $"Request to {context.Request.Path} took {watch.ElapsedMilliseconds} ms";
        Console.WriteLine(log);
    }
}
  • What is Kestrel in ASP.NET Core?
    Kestrel is a cross-platform web server for ASP.NET Core based on the libuv library, the same one used by Node.js. Kestrel is typically run behind a production reverse proxy server such as Nginx or IIS.
  • What is a Startup class in ASP.NET Core?
    The Startup class is the entry point for an application, and it’s responsible for startup and configuration, including middleware, services, and more. It must include a Configure method for setting up the application’s request handling pipeline with IApplicationBuilder, and optionally a ConfigureServices method to add services to the IServiceCollection.
  • Can you give a basic example of a Startup class in ASP.NET Core?
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
  • What is Dependency Injection in .NET Core?
    Dependency Injection (DI) is a design pattern that allows us to eliminate hard-coded dependencies and make our applications loosely coupled, extendable, and maintainable. .NET Core includes built-in support for DI.
  • What is the ‘Program’ class in .NET Core?
    The ‘Program’ class in a .NET Core application is the entry point for the application. The Main method in the ‘Program’ class is the first method that gets invoked when an application is run.
  • What are some common types of Middleware in ASP.NET Core?
    Some common middleware components in ASP.NET Core include Static Files Middleware, Authentication Middleware, Routing Middleware, Session Middleware, MVC Middleware, etc.
  • How do you serve static files in ASP.NET Core?
    Static files are served using the UseStaticFiles middleware in the Configure method of the Startup class.
public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
}
  • What are Tag Helpers in ASP.NET Core?
    Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. They’re a new feature in ASP.NET Core MVC that provides an HTML-friendly development experience.
  • What is Razor Pages in ASP.NET Core?
    Razor Pages is a new feature of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. Razor pages use the .cshtml file extension and can contain both HTML and C# code.
  • What is the use of appsettings.json in ASP.NET Core?
    The appsettings.json file in ASP.NET Core is used to store and retrieve the application’s configuration data.
  • What is Entity Framework Core?
    Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. It’s an Object/Relational Mapping (O/RM) framework for .NET.
  • How do you define a model class in Entity Framework Core?
    A model class in Entity Framework Core is a regular C# class. EF Core can work with it to create a database table that corresponds to this class.
public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Course { get; set; }
}
  • How do you create a database context in Entity Framework Core?
    A database context represents a session with the database and can be used to query and save instances of your entities. Here’s an example:
public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options)
        : base(options)
    { }

    public DbSet<Student> Students { get; set; }
}
  • What is a Controller in ASP.NET Core MVC?
    A Controller in ASP.NET Core MVC is a class that handles browser requests. It retrieves data from a model and returns a view that displays that data.
  • What is a View in ASP.NET Core MVC?
    A View in ASP.NET Core MVC is essentially a template that is used to generate an HTML response. It can use Razor syntax to include server-side code.
  • What is a Model in ASP.NET Core MVC?
    A Model in ASP.NET Core MVC represents the state of the application. This could be database records, files, or any other kind of data your application is working with.
  • What is Razor syntax in ASP.NET Core?
    Razor syntax is a server-side markup language that is used to create dynamic web pages with C# or Visual Basic .NET. Razor is used with MVC to create views.
  • Can you give a basic example of a Controller in ASP.NET Core MVC?
    Here’s an example of a simple controller:
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}
  • What is the ‘IActionResult’ return type in ASP.NET Core MVC?
    IActionResult is an interface that can be returned from an action method in ASP.NET Core MVC. It represents a command that can be executed resulting in a HTTP response. It can be a view, file, status code, or a redirect to another action or controller.
  • What are Action Filters in ASP.NET Core MVC?
    Action filters in ASP.NET Core MVC allow you to run code before or after specific stages of the request processing pipeline. They’re used to handle cross-cutting concerns like logging, caching, authorization, and exception handling.
  • What are ASP.NET Core Identity and its benefits?
    ASP.NET Core Identity is a membership system that provides login functionality for your application. It includes features for managing users, passwords, profile data, roles, claims, tokens, email confirmation, and more. It helps to secure your application and manage its users.
  • What is scaffolding in ASP.NET Core?
    Scaffolding in ASP.NET Core is a technique where the basic code structure for a web application can be automatically generated. This can be very useful for generating boilerplate code for common tasks such as creating CRUD operations for a database table.
  • What is a ViewComponent in ASP.NET Core MVC?
    A ViewComponent is similar to a Partial View but it’s more powerful. It can have its own model binding, and it’s reusable across views. It’s perfect for creating widgets in your application.
  • What is Blazor in .NET Core?
    Blazor is a framework in .NET Core that allows developers to build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS.
  • What is SignalR in .NET Core?
    SignalR is a library that simplifies adding real-time web functionality to applications. This real-time functionality allows server-side code to push content updates to connected clients instantly.
  • What is gRPC in .NET Core?
    gRPC is a high-performance, open-source, universal remote procedure call (RPC) framework. In .NET Core, you can build gRPC services that can be consumed by other services or clients.
  • What is Swagger in .NET Core?
    Swagger is an open-source tool that helps developers design, build, document, and consume RESTful web services. In .NET Core, you can use the Swashbuckle package to integrate Swagger.
  • What are the main benefits of .NET Core?
    The main benefits of .NET Core are: it’s cross-platform, high performance, supports microservices architecture, it’s open source and has a strong support for cloud development.
  • How do you add a package using NuGet in .NET Core?
    To add a package using NuGet, you can use the dotnet add package command. For example, to add the Newtonsoft.Json package, you can use the following command: dotnet add package Newtonsoft.Json
  • What is NuGet? NuGet is a free and open-source package manager designed for the Microsoft development platform. It’s a central place for sharing and consuming libraries, tools, and SDKs in .NET.
  • What is the launchSettings.json file in .NET Core?
    The launchSettings.json file is a configuration file for the .NET Core CLI. It specifies the command to execute when you start debugging your app and configures other debug settings.
  • What is .NET Standard?
    .NET Standard is a specification of .NET APIs that are intended to be available on all .NET implementations. The goal of .NET Standard is to enable .NET developers to write libraries that can be used, unchanged, on any .NET platform.
  • What is LINQ in .NET Core?
    Language Integrated Query (LINQ) is a feature in .NET Core that makes it easier to work with arrays, collections, and databases. It provides a set of query operators to query, project and filter data in arrays, enumerable classes, relational databases, XML, and more.
  • What is C#?
    C# (pronounced “C Sharp”) is a modern, object-oriented programming language that was developed by Microsoft to compete with Java. It’s the primary language for developing on the .NET platform.
  • What is the Razor View Engine?
    Razor is a markup syntax for embedding server-based code into web pages. The Razor syntax is a template markup syntax, based on the C# programming language, that the view engine used in ASP.NET MVC and ASP.NET Web Pages uses to dynamically generate web pages.
  • What is the role of the ConfigureServices method in the Startup class of ASP.NET Core?
    The ConfigureServices method is used to add and configure services. A service is a reusable component that provides app functionality. Services are registered in ConfigureServices and consumed across the app via dependency injection or ApplicationServices.
  • What is an ActionResult in ASP.NET Core MVC?
    An ActionResult is an abstract class in ASP.NET Core MVC that represents the result of an action method. It can represent different types of responses, such as views, file downloads, redirections, or a simple message.
  • How do you handle exceptions in ASP.NET Core MVC?
    In ASP.NET Core MVC, you can handle exceptions by using try-catch blocks, exception filters, or by using middleware. There’s a built-in middleware for handling exceptions in ASP.NET Core called UseExceptionHandler.
  • What is a Route in ASP.NET Core MVC?
    A route in ASP.NET Core MVC is a URL pattern that is mapped to a handler. The handler can be a physical file, such as a .aspx file in a Web Forms application, or a controller action in an MVC application.
  • What is Model Binding in ASP.NET Core MVC?
    Model Binding in ASP.NET Core MVC maps data from HTTP requests to action method parameters. The parameters may be simple types such as strings, integers, or floats, or they may be complex types, which are typically defined as classes with properties of complex types.

RELATED POSTS

View all

view all