ASP.NET Core interview questions and answers

1)What is IWebHost interface in ASP.NET Core?

IWebHost is one of the building block of ASP.NETCore , It contains application configuration and kestrel server.

2)What is the difference between Program class and Startup class?

-Program class is used to define required infrastructure to an application like Application settings , Logging and Web Server  related etc..
-Program class required changes rarely.

-Startup class is used to define application components and features etc.
-Startup class required changes frequently.

3)What is  ConfigureServices and Configure methods in Startup class in ASP.NET Core?

-ConfigureServices method is used to registering the service with IServiceCollection
-Configure method is used to configure the middleware pipeline for handling HTTP requests.

Services those are configured in ConfigureServices methods those are available to Configure method.

public class Startup
{
Public void ConfigureServices(IServiceCollection service)
{
//here registering the service with IServiceCollection.
}
Public void Configure(IApplicationBuilder ap)
{
//here configure the middleware pipeline for handling HTTP requests.
}
}


4)What is WebServer used in ASP.NET Core?

By default ASP.NETCore using Kestrel webserver.

5)What is ASP.NET Core application life cycle?

All clients requests goes to web server (IIS/Apache/Ngix etc), from web server to ASP.NET Core Server that is either Kestrel or other , From here to application middle ware, Here requests are handled and response generated , this response transferred to client vice versa to request.    

6)What is HttpContext object in ASP.NET Core?

HttpContext object created by ASP.NET Core webserver , Its contains the information about request specific  services , configuration details, data loads and errors information and it is passes to entire application.

7)What is middleware pipeline in ASP.NETCore?.

In ASP.NetCore middleware is C#.Net class handle HTTP request or response.

It is responsible to transfer request or response to the  set of components that is execute in a sequence order.

In Startup class Cofigure method is used to define middleware.

In  middleware components order is important , In given order only components execution will be done.

Middleware able to call another and this middleware calls another middleware and so on.  This is called pipeline.

8)What is ViewModel?

ViewModel is a simple object contains the data required by the view to render in UI. 


9)What is difference between Httphandler , Httpmodules and ASP.NET Core Middleware?

Httphandler process the request and send response back to browser.
Httpmodules handle  the requests based events, but should have complete understanding on request state.
.NET Core middleware pipeline request transferred between  different middlewares generate response. Pipeline is define in code so that we have control over request transition. you can change order whenever you want.

10)What is wwwroot folder in ASP.NETCore?

Image files placed and loaded from wwwroot folder.

11)Why ExceptionHandler middleware place in  the first in ASP.NETCore?

-ASP.NETCore middleware pipeline is always bidirectional.
-Request first enter into the ExceptionHandler middleware, While entering it is almost ignored , but while sending the response it is the last middleware so that If response contains any errors, modify the response and send a friendly message back to the client or reexecute pipeline again. 
-If ExceptionHandler not handling the errors webserver returns a statuscodes to the user.

12)What is the error handling namespaces in ASP.NETCore?

Microsoft.AspNetCore.All
Microsoft.AspNetCore.Diagnostics

13) What is the app.UseDeveloperExceptionPage() middleware  in ASP.NETCore?

-If we want to get full information about exceptions(Line number + Exception information+request that caused + Fullstack trace) while developing the application you can use this middleware. 
-This middleware using DeveloperExceptionPageMiddleware class.

14)What is the app.UseStatusCodePages() middleware  in ASP.NETCore?.

Using this middleware you can get different response for different  statuscodes. 

15)When do we need to return Status code as a response?

If you are building API for SPA or other application, if something went wrong in API side returning HTML response is not good  so that returning status codes to client. 

16)How can we disable middleware in ASP.NETCore?

Using HttpContext class Features property.

Example:

Public void SCode()
{
var sCodeFeature = HttpContext.Features<IStatusCodePagesFeatures>();
if(sCodeFeature != null)
{
sCodeFeature.Enabled = false;
}
}

17)How to add middleware in ASP.NET Core applications?

In two ways we are adding this 
1)Using nuget package manager.
2)By editing project file.

18) Where we define Route in ASP.NETCore?

Define route in Startup class configure method.

19)What is TagHelpers in ASP.NETCore ?

-Using TagHelpers able to render server side code in HTML elements in Razor files.
-Diffrent kind of TagHelpers available for different purposes.

Example:
Anchor TagHelper , Environment TagHelper Etc.
<label asp-for="College.DeptName"></label>


20)What does it mean of  self closing HTML elements?

closing also part same element is know as self closing elements.

<email mail-to="ShivaGummadidala@a.com" />

21) What is FormTagHelper in ASP.NETCore?

-FormTagHelper used to generate action attribute depends given data.
-By default predominately generating hidden field for preventing CSRF attacks.

CSRF token name is like "_RequestVerificationToken" 

<form asp-action="GetTransport" asp-controller="GetData"  asp-route-rno="5">

For above syntax generating action attribute like GetData/GetTransport/5

22) What is Cache busting?

-Caching is mechanism used to increase performance an application.

-With caching , all browsers cache files locally and reuse them for subsequent requests 

-Caching good idea , If an application has static content and not changed frequently, in case application has frequent changes then it will create an issue.

"Cache busting solve the browser caching issue if your application has frequent changes"

-To avoid above issue appending parameter in URL like ?v=1 , browser caching the based on URL, If application changes needs to query parameter ?v=2 again changing parameter for every change in application needs to manual intervention and time taking processes 

23) How Cache busting handled in ASP.NETCore?


- Using app-append-version = 'true' attribute you can handle cache busting in ASP.NETCore.
- app-append-version attribute generate a unique hashcode will be  generated  for the server side resources , If resource got changed new hash code will be generated so that query string also getting changed 
-Depends on hashcode browser handle the requests and responses.
-You can apply this attribute to <script><img><link>

24) What is Ok() helper method in ASP.NETCore?

-Ok helper method  return 200 statuscode.
-This statuscode return when requested data found.
-If you want you can return the data, 

25) What is NotFound() helper method in ASP.NETCore?

-NotFound helper method return 404 status code.
-This statuscode return when requested data not found.

26) What is BadRequest() helper method in ASP.NETCore?

-BadRequest helper method  return 400 statuscode.
-This statuscode return when data provided in the request failed the validation.

27)Can we apply Conventional and Attribute routing on one application?

-Yes we can apply
-But always attribute routing bypass conventional routing.

28) Do we call single action method for multiple URL's in ASP.NETCore?

-Yes, you can call by applying multiple route attributes in single action method.

Example:

[Route("students/cse")]
[Route("students/it")]
public IActionResult GetDetails()
{

}
above action method GetDetails  called for bot "students/cse" ,"students/it" routes

29)Why attribute routing best fit for ASP.NETCore WebAPI?

-Conventional routing defined in Stratup.cs file.
-Conventional routing evaluation always following order top to down, if first route matching control goes to that controller action method. If order is missing application will give unexpected results 
-Due to above approach always specify more specific routes in first and general routes at last. 

-Many Web API application has multiple endpoints , If we define routing in conventional manner there may be chance for URL conflicts, , needs to  do lots of work on ordering the routes. so that attribute routing is best fit for API.

30)How to add global prefix to attribute routing in ASP.NETCore?

By applying route attribute to base class of controller 

[Route("api")]
public class BaseController : Controller
{

}

[Route("dept")]
public class ChildController : BaseController 
{
[Route("cse")]
public IActionResult GetDetails()
{
//method def
}
}

api prefixing to the URL - api/dept/cse


For Part-2   click here
For TagHelpers FAQs click here

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers

1 comment: