Interview Questions and Answer
Interview Questions and Answer
Interview Questions and Answer
© Copyright by Interviewbit
Contents
Conclusion
32. Conclusion
Both the ASP.NET and ASP.NET Core run on C#, an object-oriented, general-purpose
programming language. ASP.NET Core inherits many concepts and features from its
ASP.NET heritage, but it’s fundamentally a new framework.
Though Microso is going to support the legacy ASP.NET framework, it’s not going to
develop it actively. The new ASP.NET Core framework will include all the new features
and enhancements. Going forward, Microso is recommending developers to build
all the new web applications with ASP.NET Core instead of the legacy ASP.NET
framework.
In this article, we will focus on both ASP.NET and ASP.NET Core interview questions.
To limit the article’s scope, we assume that you have programmed in the C#
programming language. A basic understanding of common object-oriented concepts
and front-end technologies such as HTML, CSS, and JavaScript is also expected.
We have divided the interview questions into two sections. The basic interview
questions cover the fundamentals and focus on understanding the application
structure of a basic ASP.NET project. Then, we cover the more advanced concepts
such as dependency injection, routing, and model binding in the advanced interview
questions.
For example, let’s say you ask a question on Stack Overflow. Initially, you will see only
your question when you visit the URL. However, if another user answers your
question, the browser will display that answer on your next visit to the same URL.
A web application consists of multiple separate layers. The typical example is a three-
layered architecture made up of presentation, business, and data layers. For
example, the browser (presentation) talks to the application server, which
communicates to the database server to fetch the requested data.
The following figure illustrates a typical Web application architecture with standard
components grouped by different areas of concern.
Though it’s a better choice in almost all the aspects, you don’t have to switch to
ASP.NET Core if you are maintaining a legacy ASP.NET application that you are happy
with and that is no longer actively developed.
ASP.NET MVC is a better choice if you:
Don’t need cross-platform support for your Web app.
Need a stable environment to work in.
Have nearer release schedules.
Are already working on an existing app and extending its functionality.
Already have an existing team with ASP.NET expertise.
On the so ware side, a web server is a program that accepts HTTP requests from the
clients, such as a web browser, processes the request, and returns a response. The
response can be static, i.e. image/text, or dynamic, i.e. calculated total of the
shopping cart.
Popular examples of web servers include Apache, Nginx, IIS.
In an application that follows the MVC pattern, each component has its role well
specified. For example, model classes only hold the data and the business logic. They
don't deal with HTTP requests. Views only display information. The controllers
handle and respond to user input and decide which model to pass to which view. This
is known as the separation of responsibility. It makes an application easy to develop
and maintain over time as it grows in complexity.
// Models/Post.cs
namespace app.Models
{
public class Post
{
public int ID { get; set; }
View: Represents all the UI logic of the application. In a web application, it represents
the HTML that's sent to the user and displayed in the browser.
One important thing to remember is that this HTML is not static or hard-coded. It's
generated dynamically by the controller using a model's data. In ASP.NET, the 'Views'
directory contains the views in files ending with the .cshtml file extension.
To continue our example of a blog post, a view to render a post might be:
// Views/Post.cshtml
<div class="post">
<div class="title">
<a href="/posts/@post.ID">@post.Title</a>
</div>
<div class=body>
@Html.Raw(post.Body)
</div>
</div>
Controller: Acts as an interface between Model and View. It processes the business
logic and incoming requests, manipulates data using the Model, and interacts with
the Views to render the final output.
In ASP.NET, these are C# classes that form the glue between a model and a view. They
handle the HTTP request from the browser, then retrieve the model data and pass it
to the view to dynamically render a response. The 'Controllers' directory stores the
controller classes.
A PostController that builds the view for the post by fetching the Post model will be:
// Controllers/PostController
namespace app.Controllers
{
public class PostsController : BaseController
{
public IActionResult Post(int id)
{
// Get the post from the database
Post post = _service.Get(id);
The project file is one of the most important files in our application. It tells .NET how
to build the project.
All .NET projects list their dependencies in the .csproj file. If you have worked with
JavaScript before, think of it like a package.json file. The difference is, instead of a
JSON, this is an XML file.
When you run dotnet restore, it uses the .csproj file to figure out which NuGet
packages to download and copy to the project folder (check out the next question to
learn more about Nuget).
The .csproj file also contains all the information that .NET tooling needs to build the
project. It includes the type of project you are building (console, web, desktop, etc.),
the platform this project targets, and any dependencies on other projects or 3rd
party libraries.
Here is an example of a .csproj file that lists the Nuget packages and their specific
versions.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.5.6.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Npgsql" Version="5.0.1.1" />
<PackageReference Include="Serilog" Version="2.10.0" />
</ItemGroup>
</Project>
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"AppConnection": ""
},
"AWS": {
"Profile": "local-test-profile",
"Region": "us-west-2"
},
"AllowedHosts": "*"
}
15. What is IIS?
IIS stands for Internet Information Services. It is a powerful web server developed by
Microso . IIS can also act as a load balancer to distribute incoming HTTP requests to
different application servers to allow high reliability and scalability.
It can also act as a reverse proxy, i.e. accept a client’s request, forward it to an
application server, and return the client’s response. A reverse proxy improves the
security, reliability, and performance of your application.
A limitation of IIS is that it only runs on Windows. However, it is very configurable. You
can configure it to suit your application’s specific needs.
16. What is Kestrel?
Kestrel is an open-source, cross-platform web server designed for ASP.NET Core.
Kestrel is included and enabled by default in ASP.NET Core. It is very light-weight
when compared with IIS.
Kestrel can be used as a web server processing requests directly from a network,
including the Internet.
Though Kestrel can serve an ASP.NET Core application on its own, Microso
recommends using it along with a reverse proxy such as IIS, Nginx, or Apache, for
better performance, security, and reliability.
18. What is caching?
Caching is the process of storing data in a temporary storage location that is quicker
to access than the original location of the data so that it can be accessed more
quickly when the same data is needed next time.
Caching improves the scalability and performance of your application. It does this by
reducing the work required to fetch the data. Caching is useful for data that doesn’t
change frequently and is expensive to create and retrieve.
ASP.NET provides a set of caching features out of the box. You can use the
IMemoryCache interface for simple use cases. It represents a cache stored in the web
server’s memory. ASP.NET also supports distributed caching, which is a cache shared
by multiple app servers, with Redis.
[HttpGet("posts/{id}")]
public ActionResult<Post> GetById(int id, bool archivedOnly)
http://yourapp.com/api/Posts/5?ArchivedOnly=true
A er the routing selects the action method, model binding executes the following
steps.
Locate the first parameter of GetByID, an integer named id, look through the
available sources in the HTTP request and find id = "5" in route data.
Convert the string "5" into an integer 5.
Find the next parameter of GetByID, a boolean named archivedOnly.
Look through the sources and find "ArchivedOnly=true" in the query string. It
ignores the case when matching the parameters to the strings.
Convert the string "true" into boolean true.
Some other examples of attributes include:
1. [FromQuery] - Gets values from the query string.
2. [FromRoute] - Gets values from route data.
3. [FromForm] - Gets values from posted form fields.
4. [FromBody] - Gets values from the request body.
5. [FromHeader] - Gets values from HTTP headers.
}
}
...
return View();
}
}
25. What is a cookie?
A cookie is a small amount of data that is persisted across requests and even
sessions. Cookies store information about the user. The browser stores the cookies
on the user’s computer. Most browsers store the cookies as key-value pairs.
Write a cookie in ASP.NET Core:
Response.Cookies.Append(key, value);
In each of these examples, the middleware receives a request, modifies it, and then
passes it to the next middleware piece in the pipeline. Subsequent middleware uses
the details added by the earlier middleware to handle the request in some way. The
following diagram illustrates this.
There are two ways to define routes in an ASP.NET Core MVC application.
Conventional Routing
Attribute-Based Routing.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
This code creates a single route named ‘default’. The route template pattern
‘{controller=Home}/{action=Index}/{id?}’ is used to match an incoming URL such as
/Posts/Archived/5 to the Archived(int id) action method on the PostsController,
passing 5 for the id parameter. By default, the router uses the Index method on the
HomeController.
[Route("blog")]
public class PostsController : Controller
{
[HttpGet("post/{id:int}")]
public IActionResult Show(int id = 0)
{
Post post = new Post()
{
ID = id
};
[HttpGet("edit/{id:int}")]
public IActionResult Edit(int id)
{
Post postToEdit = _service.Get(id);
Not all web applications return an HTML view as a response to an HTTP request.
Sometimes, a client only wants some data from your application, and it wants to
handle how that data will be formatted.
For example, let's say your application supports both web and mobile interfaces.
Instead of writing two separate projects which return HTML and mobile views, you
can write a single application that only returns the specific data that the clients need.
Once the clients receive this data, they format it accordingly. The web client renders
the HTML using view templates and JavaScript, and the mobile clients generate the
appropriate mobile view for its specific platform.
An application might also need to communicate with another application to fetch
the data that it needs. For example, when you go to Amazon.com, it communicates
with hundreds of other services and applications to retrieve data and renders the
final HTML page you see.
Such back-end applications, which provide data, are commonly known as RESTful
web services. REST protocol uses verbs like GET, POST, PUT, DELETE to
communicate between multiple applications. The client and server can be written in
different languages and technologies and still work together without knowing about
each other, as long as each side knows the format of the data that is getting sent.
ASP.NET Core supports creating RESTful services, also known as web APIs, using C#. A
Web API consists of one or more controllers that derive from ControllerBase class.
[PostController]
[Route("[controller]")]
public class PostController : ControllerBase
An MVC controller derives from the Controller class. However, a Web API controller
should derive from the ControllerBase class. The reason is that Controller derives
from ControllerBase and provides additional support for views, which you don’t need
for web API requests.
That said, you can use a controller for both rendering views and data. That is, it can
act as both an MVC controller and a Web API controller. In this case, you can derive
the controller from the Controller class.
Conclusion
32. Conclusion
In this article on ASP.NET interview questions, we learned about the legacy ASP.NET
framework and its modern alternative, that is ASP.NET Core. The article explored a
broad range of basic and advanced questions that an interviewer would ask in a job
interview for a junior/intermediate developer role. We hope it helps for your next job
interview!
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions