ASP.NET CORE Interview Questions & Answers

1. What is the ASP.NET Core?

ASP.NET Core is an open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It's cross-platform (supporting Windows, macOS, and Linux) and can be used to build devices, cloud, and IoT applications. ASP.NET Core is not an upgraded version of ASP.NET. ASP.NET Core is completely rewriting that work with .net Core framework. It is much faster, configurable, modular, scalable, extensible and cross-platform support. 

2. Features provided by ASP.NET Core


1. Built-in supports for
2. Dependency Injection
3. Built-in supports for the logging framework and it can be extensible
4. Introduced new, fast, and cross-platform web server - Kestrel. So, a web application can run without IIS, Apache, and Nginx.
5. Multiple hosting ways are supported
6. It supports modularity, so the developer needs to include the module required by the application. However, .NET Core framework is also providing the meta-package that includes the libraries
7. Command-line supports to create, build and run the application
8. There is no web.config file. We can store the custom configuration into an appsettings.json file
9. There is no Global.asax file. We can now register and use the services into startup class
10. It has good support for asynchronous programming
11. Support WebSocket and SignalR
12. Provide protection against CSRF (Cross-Site Request Forgery)

3. Advantages of ASP.NET Core over ASP.NET

1. There is no dependency on framework installation because all the required dependencies are ship with our application.
2. ASP.NET Core can handle more request than the ASP.NET
3. Multiple deployment options available withASP.NET Core
4. It is cross-platform, so it can be run on Windows, Linux, and Mac.

4. Differences Between .net Core and .net Framework

NuGet-based: 
.NET Core is distributed as a set of NuGet packages that allow app-local deployments. In contrast, the .NET-Framework is always installed in a system-wide location. This difference doesn’t matter so much for class libraries, but it matters for applications as those are expected to deploy the closure of their dependencies. But we expect this model to change how quickly class library authors can take advantage of new functionality. Since the applications can simply deploy a new version (as opposed to having to wait until a given .NET Framework version is widely adopted), there is less of a penalty for component authors to take advantage of the latest features.

Well layered: 
.NET Core was specifically designed to be layered. The goal was to create a .NET stack that can accommodate a wide variety of capabilities and system constraints without forcing customers to recompile their binaries and/or produce new assets. This means that we had to remove certain APIs because they tied lower-level components to higher-level components. In those cases, we provide alternatives, often in the form of extension methods.

Free of problematic tech: 
ASP.NET Core doesn’t include certain technologies we decided to discontinue because we found them to be problematic, for instance, AppDomain and sandboxing. If the scenario still makes sense for .NET Core, our plan is to have replacements. For example, AssemblyLoadContext replaces AppDomains for loading and isolating assemblies.

5. Use of .csproj File 

  • csproj file is now used as a place where we manage the NuGet packages for your application.
  • File explorer and project explorer are now in sync. For .NET Core projects, you can easily drop a file from file explorer into a project or delete it from the file system and it will be gone from the project. No more source files in a .csproj file.
  • You can now edit the .csproj file directly without unloading the project.

6.  Startup class in ASP.NET core


Startup class is the entry point of the ASP.NET Core application. Every .NET Core application must have this class. This class contains the application configuration rated items. It is not necessary that the class name must "Startup", it can be anything, we can configure startup class in Program class.
  • ConfigureServices: This is an optional method of startup class. It can be used to configure the services that are used by the application. This method calls first when the application is requested for the first time. Using this method, we can add the services to the DI container, so services are available as a dependency in controller constructor.
  • Configure method: It defines how the application will respond to each HTTP request. We can configure the request pipeline by configuring the middleware. It accepts IApplicationBuilder as a parameter and also it has two optional parameters: IHostingEnvironment and ILoggerFactory. Using this method, we can configure built-in middleware such as routing, authentication, session, etc. as well as third-party middleware.
  • Middleware: It is software which is injected into the application pipeline to handle request and responses. They are just like chained to each other and form as a pipeline. The incoming requests are passes through this pipeline where all middleware is configured, and middleware can perform some action on the request before passes it to the next middleware. Same as for the responses, they are also passing through the middleware but in reverse order.

7. Routing in ASP.NET Core


Routing is functionality that map incoming request to the route handler. The route can have values (extract them from URL) that used to process the request. Using the route, routing can find route handler based on URL. All the routes are registered when the application is started. 

There are two types of routing supported by ASP.NET Core: 1)  The conventional routing 2) Attribute routing

The Routing uses routes for map incoming request with route handler and Generate URL that used in response. Mostly, the application having a single collection of routes and this collection are used for the process the request. The RouteAsync method is used to map incoming request (that match the URL) with available in route collection.

8. Enable Session in ASP.NET Core


The middleware for the session is provided by the package Microsoft.AspNetCore.Session. To use the session in ASP.NET Core application, we need to add this package to csproj file and add the Session middleware to ASP.NET Core request pipeline.

public class Startup
{
 public void ConfigureServices(IServiceCollection services)
 {
    services.AddSession();
  services.AddMvc();
 } 
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
  app.UseSession();
 }
}

9. Tag helper in ASP.NET Core


It is a feature provided by Razor view engine that enables us to write server-side code to create and render the HTML element in view (Razor). The tag-helper is C# classes that used to generate the view by adding the HTML element. The functionality of tag helper is very similar to HTML helper of ASP.NET MVC.

 Example:
 //HTML Helper
 @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", placeholder = "Enter Your First Name" }) 
 
 //content with tag helper
 <input asp-for="FirstName" placeholder="Enter Your First Name" class="form-control" /> 
 
 //Equivalent HTML
 <input placeholder="Enter Your First Name" class="form-control" id="FirstName" name="FirstName" value="" type="text"> 


Post a Comment

If you have any questions or concerns, please let me know.