Next 9 ASP.NET MVC Interview Questions and Answers for the year - 2022 - USMTECHWORLD.

1. What is Controller Actions in ASP.NET MVC?

Answer:
A controller has many actions.An action is a method in the controller which will be called when you enter particular URL in the browser. For eg:- http://localhost/Customer/Index Here Index() is an action on the CustomerController class.

2. What are Action results in ASP.NET MVC?

Answer:
The controller action returns return type,which is called action result.This action result is returned as the response by the browser request.
There are many types of action results.
1.ViewResult - This has HTML and markup.
2.EmptyResult - This returns no result.
3.RedirectResult - This redirect to new URL.
4.JsonResult - This returns JSONRESULT that can be used in an AJAX application.
5.JavaScriptResult - This returns javascript script.
6.ContentResult - This returns a text result.
7.FileContentResult= This returns downloadable file with binary content.
8.FilePathResult - This returns downloadable file with path.
9.FileStreamResult - This returns downloadable file with file stream.
The above actonresults inherit from the base class ActionResult.
The above actionresult are not returned directly by the action,instead you can call any of the methods of the Controller base class which is abstract in nature.
1.View - Returns a ViewResult action result.
2.Redirect - Returns a RedirectResult action result.
3.RedirectToAction - Returns a RedirectToRouteResult action result.
4.RedirectToRoute - Returns a RedirectToRouteResult action result.
5.Json - Returns a JsonResult action result.
6.JavaScriptResult - Returns a JavaScriptResult.
7.Content - Returns a ContentResult action result.
8.File - Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

public ActionResult Index()  
{  
bool isLoginPassed;
if (isLoginPassed)  
{  
return Content("Login Successfull");  
}  
else  
{  
return View();  
}  
}  

3. What is a Route Constraint in ASP.NET MVC?

The route constraint means to introduce some extra validations to the specified route.This can be easily understand by this famous example of allowing only numeric values to the route or REST URL. if you enter the below route in URL. http://localhost/home/index/10 it will simply returns some results in the browser view But in the above URL, there is no constraint to pass only numeric values,you can pass string values also in the above url,but when you execute the below URL, you will face error like "The resource could not be found" http://localhost/home/index/abc

public void index(int id)
{

}
The routeconfig.cs has following route as default route.
            
routes.MapRoute("Default",
"{controller}/{action}/{id}"
"new {controller="home",action="Index",id=Urlparameter.Optional}"
);

so the above routing allows you to pass any datatype as parameter to the action method. So Here you need to add the routing constraint like the following.
            
routes.MapRoute("Default",
"{controller}/{action}/{id}"
"new {controller="home",action="Index",id=Urlparameter.Optional}",
new {id=@"\d+"}
);

4. What are the advantages and disadvantages of using sessions?

Advantages: Suppose if you have requirement to use a variable in all pages,then your mind says to store it in session.It stores every user data seperately. it is easy to store any kind of an object like datatable,dataset etc.It stores session as in memory object and it can be accessed fastly.
Disadvantages: As the session data are stored in memory,when you restart the server the session data will be lost.It will affect the performance of the large webapplications. When you store many large objects in webapplication, it will occupy more memory and it leads to slowdown of your webapplication. If your webapplication or domain is restarted, sessions will be lost.In case if you want to store in stateserver or sqlserver mode, you need to serialize or deserialize before store.

5. What are the different types of session states in ASP.NET?

6. What are the different types of Routing in ASP.NETMVC?

There are two types of routing in ASP.NET MVC.


Convention based Routing:- This is also pattern matching system for URL that map incoming request to particular controller and action.

routes.MapRoute(  
    name: "Default",  
    url: "{controller}/{action}/{id}",  
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
); 

Attribute based routing:
Here we define all our routes in controller or action methods. For registering the attribute routing in our application,we need to add the following line in routeconfig.cs routes.MapMvcAttributeRoutes();

public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
routes.MapRoute(  
name: "Default",  
url: "{controller}/{action}/{id}",  
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
); 
}

How to use Attribute routing?
use routeprefix or route attribute for attribute routing in mvc,


[RoutePrefix("Home")]
public class HomeController : Controller
{
[Route("Index")]
public ActionResult Index()
{
return View();
}
}  

7. Difference between Stateless and Statefull?

8. Whether REST API is Stateless or Statefull?

The REST API are stateless because the server expects all the information which is necessary to carry out the request from the client.So here client sends all the necessary information through the request to Server.

9.Application State Vs Resource State?

Application State: This lives on the client.It means the client which sends the request to the server carrying all the server side data such as "Previous interaction details",current context information and identification of current incoming requests. It is a server side data that server stores to identify the incoming requests,previous interaction details and current context information.
Resource State: This lives on the Server.It is the current state of the resource on a server at any point of time. It is API RESPONSE that we get from the Server.It is called Resource Representation.
A web service only needs to care about your application state when you’re actually making a request. The rest of the time, it doesn’t even know you exist. This means that whenever a client makes a request, it must include all the application states the server will need to process it.

<<Part 1 of ASP.NET MVC Q&A