Total Page Preview:   000000000427

Top 30 Asp.Net Core MVC Interview Questions Answers for freshers and experienced

 
Question-1 What are the benefits of using ASP.NET Core over ASP.NET?
Answer:ASP.NET Core comes with the following benefits over ASP.NET.
  • Cross platform, provide ability to develop and run on Windows, Linux and MacOS.
  • Open-source
  • Unified Platform to develop Web UI and services.
  • Built-in dependency injection.
  • Ability to deploy on more than one server like IIS, Kestrel, Nginx, Docker, Apache etc
  • cloud enabled framework, provide support for environment based configuration systems.
  • Lightweight, High performance and modern HTTP request pipelines.
  • well suited architecture for testability
  • Integration of many client-side frameworks like Angular any version
  • Blazor allow you to use C# code in browser with JavaScript code.
 
Question-2 What are the various JSON files available in ASP.NET Core?
Answer:There are the following JSON files in ASP.NET Core :
  • global.json
  • launchsettings.json
  • appsettings.json
  • bundleconfig.json
  • bower.json
  • package.json

Question-3 What are Metapackages?

Answer:The framework .NET Core 2.0 introduced Metapackage which includes all the supported packages by ASP.NET code with their dependencies into one package. It helps us to do fast development as we don't require to include the individual ASP.NET Core packages. The assembly Microsoft.AspNetCore.All is a meta package provided by ASP.NET core.

In other words, the Metapackages of .NET Core describes the set of packages that are used together and acts as a parent of the child grouping structure. The Metapackages are referenced just like any other NuGet package naming convention such as "NETStandard.Library". An by referencing the meta-package, you have, then all its child packages will be having the reference of its dependent packages accordingly.
Question-4  What is the function of beforeRender() in the controller?
Answer:This function is required when we call render() manually before the end of a given action. This function is called before the view is rendered and after controller action logic. It is not used often.
Question-5 What do you know about beforeFilter() and afterFilter() functions in controllers?
Answer:beforeFilter() is run every action in the controller. It is the right situation to inspect permissions or check for an active session. At the same time, afterFilter() is called after every rendering is done and after every controller action. This is the last controller method to be run.   
Question-6 What is attribute-based routing in MVC?
Answer:We have a new attribute route in ASP.NET MVC. By using the ‘route’ attribute, URL structure can be defined. If we decorate the ‘GotoAbout' action with the route attribute, the route attribute says that 'GotoAbout’ can be invoked using the ‘Users/about’ URL structure. 
Question-7 What is the difference between ‘ViewResult’ and ‘ActionResult’?
Answer:ViewResult is derived from the 'AbstractResult' class, and 'ActionResult’ is an abstract class. ActionResult is good when you are dynamically deriving different types of views. The derived classes of ActionResult are FileStreamResult, ViewResult, and JsonResult. 
Question-8 Give the importance of NonActionAttribute.
Answer:If we want to prevent the default nature of a public method of a controller from being treated as an action method, then we assign the NonActionattribute to the public method. 
Question-9 Define MVC’s partial view.
Answer:MVC's partial view renders a portion of view content. This helps in reducing code supplication. In layman's terms, the partial view allows rendering a view within the parent view. 
Question-10 How will you implement validation in MVC?
Answer:We can implement validation in the MVC application with the help of validators defined in the System.ComponentModel.DataAnnotations namespace. The different validators are DataType, Required, Range, and StringLength. 
Question-11 What do you understand by Model Binding?
Answer:In action methods, we need to retrieve data from requests to be used by the data. In MVC, model binding maps data from HTTP request to action method parameters. This task of retrieving data from HTTPRequest is repetitive and is removed from the action method. 
Question-12 How to enable the in-memory cache in ASP.NET Core project?
Answer:You can enable the in-memory cache in the ConfigureServices method in the Startup class as shown in the code snippet below.
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddMemoryCache();
}
Question-13 State the Various Controller action ways to return Types
Answer:The various return Types for a Control action method are as follows
  • Browse Result
  • Redirect 
  • JavaScript 
  • Content-based result 
  • JSON result
Question-14 What does Model-View-Controller Stand for in MVC Application?
Answer:An MVC model uses
  • Model: It describes the data domain for the application. In simpler form, a model contains the business logic that controls how an application manages data.
  • View: It symbolizes the consumers through which final consumers interact. In essence, View contains all of the consumer logic.
  • Controller: The controller is the component that responds to consumer needs. The relevant controller responds to user activities inside the model and selects a template to display that shows the consumer. The controller houses the logic for user input.

Question-15  What distinguishes "ActionResult" from "ViewResult"?

Answer:While "ViewResult" is a class deriving from "AbstractResult," "ActionResult" is an abstract class. There are several "ActionResult" descendant classes, including "JsonResult," "FileStreamResult," and "ViewResult." If you are dynamically generating different types of views, "ActionResult" is the ideal choice.
Question-16  What is the purpose of the wwwroot folder?
Answer:The wwwroot folder contains static files and compiled assets, such as JavaScript, CSS, and images that your web application needs. Wwwroot is the only folder in the entire project that's exposed as-is to the browser.
Question-17 What is IIS?
Answer:IIS stands for Internet Information Services. It is a powerful web server developed by Microsoft. 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.
Question-18 What is a cookie?
Answer: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);
Delete a cookie in ASP.NET Core
  • Response.Cookies.Delete(somekey);

Question-19 What is the get and post action types?

Answer:In MVC, bis HttpGet and HttpPost are the methods of transferring client data to the server. They are used to post data from web pages to the server. HttpGet binds data to the URL as a query string, whereas the HttpPost doesn't bind the data to the URL and is more secure.
Question-20 How do you plan to manage sessions in MVC?
Answer:The most common approach is to use the ASP.NET Core Session middleware. This middleware stores session data in a cookie that is sent to the client with each response. The client then sends the cookie back to the server with each request, allowing the server to track the user's session across multiple requests.
 Question-21 Differentiate between View and Partial View?
Answer:Views are complete HTML pages, while partial views are reusable view components that are rendered within other views. Partial views do not contain the layout page and are typically used to display specific sections of a page.
Question-22 Write the order in which the filters are executed in ASP .NET MVC.
Answer:Filters form a part of custom classes, used to add logic that gets executed before and after a specific controller action executes. Filters execute in the following order:
  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters
Out of these four filters, the Authorization filters are run first. The exception filter is run in the end.
Question-23 What various types of results do we get in MVC?
Answer:A controller action returns an action result in response to a browser request. The action results supported by MVC include the following-
  • ViewResult 
  • EmptyResult.
  • RedirectResult
  • JsonResult 
  • JavaScriptResult.
  • ContentResult
  • FileContentResult.
  • FilePathResult
  • FileStreamResult 

Question-24 How do you handle exceptions in a .NET Core application?

Answer: In .NET Core, exceptions can be handled using try-catch blocks. When an exception is thrown, the runtime will search up the call stack for the nearest try-catch block that can handle the exception. You can also use global exception handling by registering a middleware that catches unhandled exceptions and logs them or returns an appropriate HTTP response.
Question-25 Can you provide a description of LINQ in .NET Core?
Answer:LINQ (Language Integrated Query) is a feature in .NET Core that enables developers to query and manipulate data using a declarative syntax. With LINQ, you can write queries that are similar to SQL statements, but with the added power and flexibility of C# language features. LINQ provides a variety of operators and methods that you can use to filter, sort, and transform data, making it easier and more efficient to work with collections and other data structures. LINQ is a powerful and flexible feature that enables you to write concise and expressive code in your .NET Core applications.
Describe the Service Lifetimes.
 
Question-26 When Services are registered, there is a lifetime for every service. ASP.NET Core provides the following lifetimes?
Answer:Transient - Services with transient lifetime are created each time they are requested from service container. So it's best suited for stateless, light weight services.
Scoped - Services with scoped lifetime are created once per connection or client request. When using scoped service in middleware then inject the service via invoke or invokeAsync method. You should not inject the service via constructor injection as it treats the service behavior like Singleton.
Singleton - Service with singleton lifetime is created once when first time the service is requested. For subsequent requests same instance is served by service container.
Question-27 Explain the Filters?
Answer:Filters provide the capability to run the code before or after the specific stage in request processing pipeline, it could be either MVC app or Web API service. Filters performs the tasks like Authorization, Caching implementation, Exception handling etc. ASP.NET Core also provide the option to create custom filters. There are 5 types of filters supported in ASP.NET Core Web apps or services.
  • Authorization filters run before all or first and determine the user is authorized or not.
  • Resource filters are executed after authorization. OnResourceExecuting filter runs the code before rest of filter pipeline and OnResourceExecuted runs the code after rest of filter pipeline.
  • Action filters run the code immediately before and after the action method execution. Action filters can change the arguments passed to method and can change returned result.
  • Exception filters used to handle the exceptions globally before wrting the response body
  • Result filters allow to run the code just before or after successful execution of action results.

Question-28 Describe the Servers in ASP.NET Core?

Answer:Server is required to run any application. ASP.NET Core provides an in-process HTTP server implementation to run the app. This server implementation listen for HTTP requests and surface them to the application as a set of request features composed into an HttpContext.
ASP.NET Core use the Kestrel web server by default. ASP.NET Core comes with:
  • Default Kestrel web server that's cross platform HTTP server implementation.
  • IIS HTTP Server that's in-process server for IIS.
  • HTTP.sys server that's a Windows-only HTTP server and it's based on the HTTP.sys kernel driver and HTTP Server API.

Question-29 Difference between SingleOrDefault and FirstOrDefault?

Answer:In ASP.NET Core MVC, SingleOrDefault and FirstOrDefault are LINQ extension methods that can be used to retrieve a single element from a collection based on a specified condition. These methods are commonly used when working with databases or in-memory collections.

SingleOrDefault:
SingleOrDefault returns the only element of a sequence, or a default value if the sequence is empty. If the sequence contains more than one element, or if the sequence is empty and no default value is specified, an exception is thrown.
This method is useful when you expect at most one result and want to handle the case when there are zero or one result.
Example:
FirstOrDefault:
FirstOrDefault returns the first element of a sequence, or a default value if the sequence is empty. It is similar to SingleOrDefault, but it does not throw an exception if there are multiple elements in the sequence that satisfy the condition.
Example:
In both examples, the SingleOrDefault and FirstOrDefault methods are used to query a database (assumed to be represented by a DbContext) for a specific student or a set of students based on certain conditions.
Question-30 What is the use of the "Map" extension while adding middleware to the ASP.NET Core pipeline?
Answer:It is used for branching the pipeline. It branches the ASP.NET Core pipeline based on request path matching. If the request path starts with the given path, middleware on to that branch will execute.
 
 
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: