| ASP.NET Interview Questions-11 to 20 :👈 | 👉:ASP.NET Interview Questions 31 to 40 |
ASP.NET Interview Questions 21 to 30 |
21. In ASP.NET MVC, what is the default route and where do you add it?
The default route defines how incoming URLs are mapped to controllers and action methods when no more specific route matches.
In ASP.NET MVC 5, routes are typically configured in App_Start/RouteConfig.cs and registered in Global.asax.
RouteConfig.cs:
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
For example, the URL /Products/Details/10 maps to the Details action of the ProductsController, with id = 10.
22. How do you return a string from an action controller?
In ASP.NET MVC, an action method can return a string using the Content() method or by returning a string directly from an action whose return type is string.
Recommended approach using ContentResult:
public ActionResult GetMessage()
{
return Content("Welcome to ASP.NET MVC");
}
Using a string return type:
public string GetMessage()
{
return "Welcome to ASP.NET MVC";
}
The second example returns the string as the response body. Use Content() when you want to specify the content type or encoding explicitly.
23. Can a controller have multiple views?
Yes. A controller can have multiple action methods, and each action can return a different view.
public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details()
{
return View();
}
public ActionResult Create()
{
return View();
}
}
The corresponding views are usually placed in:
Views/Employee/Index.cshtmlViews/Employee/Details.cshtmlViews/Employee/Create.cshtml
An action can also explicitly return another view using return View("ViewName").
24. What is the default identity of an application pool?
In IIS 7 and later, the default application pool identity for a newly created application pool is generally ApplicationPoolIdentity.
This creates a virtual account for the application pool, providing isolation and allowing permissions to be assigned to the individual pool.
For example, an application pool named MyAppPool can have the Windows identity:
IIS AppPool\MyAppPool
Important: Older IIS configurations commonly used Network Service as the default identity. The actual identity depends on the IIS version and application pool configuration.
25. Give two examples of scenarios when routing is not applied.
In ASP.NET MVC, routing is generally bypassed or ignored in the following situations:
- Static files: Requests for existing static files, such as images, CSS files, and JavaScript files, may be handled directly by IIS rather than by an MVC controller, depending on the IIS configuration.
- Ignored routes: Routes explicitly excluded using
IgnoreRoute()are not processed as normal MVC routes.
Example:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
This excludes matching .axd resource URLs from normal MVC route processing.
Note: Static-file handling depends on the IIS pipeline, handler mappings, and application configuration. Not every static-looking URL necessarily bypasses routing.
26. How does IIS recognize the programming language in which a web application is developed?
IIS does not generally identify a web application's programming language by examining its source code. Instead, it uses configuration, handler mappings, and the hosting runtime to determine how requests should be processed.
- Handler mappings: Map request paths or file extensions to appropriate handlers.
- ASP.NET Framework applications: Use the ASP.NET runtime and IIS integration to process applicable requests.
- Static files: Are typically served directly by IIS.
- ASP.NET Core applications: Are hosted through the ASP.NET Core Module in IIS, which forwards requests to the ASP.NET Core application.
For example, an ASP.NET Framework application can be developed using C# or VB.NET. Both can run on the same .NET runtime, so IIS does not need to distinguish between those languages for ordinary request processing.
27. How do you detect programmatically whether ViewState has been tampered with?
ASP.NET Web Forms validates ViewState integrity using a Message Authentication Code (MAC). When ViewState MAC validation is enabled, ASP.NET detects invalid or modified ViewState during request processing and normally rejects the request.
ViewState MAC validation is enabled by default in supported ASP.NET Framework configurations. Do not disable it to work around validation errors.
Example configuration:
<system.web>
<pages enableViewStateMac="true" />
</system.web>
When an attacker or another process modifies the protected ViewState, ASP.NET normally raises an exception during deserialization or validation, before the page's ordinary event-handling code runs.
You can log the exception at an appropriate error-handling boundary, such as Application_Error in Global.asax, while avoiding the disclosure of sensitive details to the client.
protected void Application_Error()
{
Exception ex = Server.GetLastError();
// Log the exception securely.
// Do not expose sensitive exception details to users.
}
Important: There is no need to implement your own ViewState tampering check. Keep MAC validation enabled, protect sensitive data independently, and configure a consistent, secure machine key across servers when required.
28. How can you keep two methods with the same action name and parameters in MVC?
C# does not allow two methods in the same class to have identical signatures. However, ASP.NET MVC can expose two differently named C# methods under the same action name using ActionNameAttribute and distinguish them using HTTP verbs.
public class HomeController : Controller
{
[HttpGet]
[ActionName("Save")]
public ActionResult SaveGet(int id)
{
return Content("GET request");
}
[HttpPost]
[ActionName("Save")]
public ActionResult SavePost(int id)
{
return Content("POST request");
}
}
Both methods use the MVC action name Save and accept an integer parameter named id. MVC selects the appropriate method according to the HTTP request method.
For production applications, POST actions that modify data should also use appropriate anti-forgery protection.
29. How do you prevent caching of a particular page in ASP.NET?
In ASP.NET MVC 5, you can use the OutputCache attribute to disable output caching for a particular action.
using System.Web.Mvc;
public class AccountController : Controller
{
[OutputCache(
Duration = 0,
NoStore = true,
Location = OutputCacheLocation.None)]
public ActionResult Profile()
{
return View();
}
}
Alternatively, you can set response cache headers programmatically:
public ActionResult Profile()
{
Response.Cache.SetCacheability(
System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
return View();
}
This instructs browsers and intermediary caches not to store the response under the specified cache directives.
Note: For sensitive pages, also review application-level caching, proxy/CDN configuration, and any custom caching middleware or infrastructure.
30. In which assembly is the MVC framework defined?
In classic ASP.NET MVC 5, the MVC framework is primarily defined in:
System.Web.Mvc.dll
This assembly contains important MVC types, including:
ControllerActionResultViewResultModelStateDictionaryAuthorizeAttribute
ASP.NET Core MVC uses different assemblies and namespaces, such as Microsoft.AspNetCore.Mvc.
| ASP.NET Interview Questions-11 to 20 :👈 | 👉:ASP.NET Interview Questions 31 to 40 |