Next 9 ASP.NET MVC Interview Questions and Answers for the year - 2026 - 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
The routeconfig.cs has following route as default route.
public void index(int id)
{
}
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}"
);
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.
10.What is Session Affinity?
https://stackoverflow.com/questions/10494431/sticky-and-non-sticky-sessions
11.What is Sticky Sessions?
The sticky session can be explained by the following scenario.Let us consider your website is maintained
by only one server.So for each client-server pair, a session object is created and remains in the memory
of the web server.All the requests from the client goes to this web server and update this session
object.
Normally we will store user related information in session object,so that it can be accessed in all other
pages.
Suppose if your website is maintained by multiple web servers which is handled by loadbalancer,
the load balancer decides which webserver should each request goes to.For example,if there are 3 webservers
A,B and C behind the loadbalancer, it is possible that www.samplesite.com is maintained from Server A,
www.samplesite.com is maintained from server B and www.samplesite.com is maintained from server C.
Since the requests is being handled by 3 webservers, each server has created a session object for you
and we dont know what is there in the session object of the other.
In order to synchronize between these server sessions,we have to read/write the session data, which is
common to all like db.
Here comes the concept of Sticky Sessions.If the load balancer is tell to use sticky sessions,all of your
interactions will happen with same server,even though other servers are present.Thus ,your
session object will be same throughout your entire interaction with this website.
Note:-
Sticky Sessions:
All your requests will be directed to the same server.
Non Sticky Sessions:The loadbalancer may choose any webserver to serve your requests.
12.what is the advantage of using loadbalancer with sticky session and session replication?
Let us consider following scenario:
1)If you use sticky session without session replication:
2)if you use sticky session with session replication:
Now we will analyse the first scenario,
1)If you use sticky session without session replication:
Suppose you have only one user using your web application, and you have 3 web servers running.
The user sends several requests to your web app,then the loadbalancer will send the first user request
to one of the 3 webservers say it is webserver A, and all the other requests sent by this user will be sent to the same
webserver A.
During these requests , if you shutdown or restart Webserver A,the loadbalancer sends the remainin
requests to one other webserver instance that is still running.
As you didnt use Session Replication,the instance of webserver which receives the remaining
requests does not have a copy of the user session,it will be a new user session and here
the user loose his session and disconnected from web app.
2)if you use sticky session with session replication:
Suppose you have only one user using web app and you have 3 webserver running.The user sends
several request to your app,then the loadbalancer will send the first user request to one of the 3
web server i.e (webserver A), and all the other requests that are sent by this user during this
session will be sent to same (webserver A).
During these requests , if you shutdown or restart this (webserver A) the loadbalancer sends remaining
requests to one other webserver that is running.
Also you have used session replication here,the instance of server which recieves the remaining
requests has copy of the user session and which keeps the webapp connected to the first
user request.
The user continue to browse your webapp without being disconnected.