| ASP-NET Interview Questions-21 to 30 :👈 | 👉:ASP-NET Interview Questions 41 to 50 |
ASP.NET Interview Questions 31 to 40 |
31. Is it possible to share a view across multiple controllers?
Yes. A view can be shared across multiple controllers by placing it in the Views/Shared folder.
Example folder structure:
Views/
Shared/
Error.cshtml
Contact.cshtml
Home/
Index.cshtml
Employee/
Index.cshtml
Any controller can return a shared view by specifying its name:
public ActionResult Error()
{
return View("Error");
}
MVC searches the controller-specific view folder and then the shared view folder when resolving conventional view names.
32. What are the different return types of a controller action method?
In ASP.NET MVC 5, controller action methods commonly return ActionResult or a class derived from ActionResult.
| Return Type | Purpose |
|---|---|
ViewResult |
Renders a view. |
PartialViewResult |
Renders a partial view. |
ContentResult |
Returns text or other specified content. |
JsonResult |
Returns JSON data. |
RedirectResult |
Redirects the client to a URL. |
RedirectToRouteResult |
Redirects to a URL generated from routing information. |
FileResult |
Returns a file to the client. |
EmptyResult |
Represents an empty action result. |
HttpStatusCodeResult |
Returns an HTTP status code. |
Example:
public ActionResult Index()
{
return View();
}
public ActionResult Message()
{
return Content("Hello");
}
public ActionResult RedirectHome()
{
return RedirectToAction("Index", "Home");
}
An MVC action can also return compatible concrete result types directly. The exact set of supported results differs in ASP.NET Core MVC.
33. What is routing in MVC?
Routing is the mechanism that maps incoming URLs to controller actions in an ASP.NET MVC application.
For example:
/Employee/Details/10
This URL can map to:
- Controller:
EmployeeController - Action:
Details - Parameter:
id = 10
Example action:
public ActionResult Details(int id)
{
return Content("Employee ID: " + id);
}
Routing allows applications to use readable URLs without exposing physical page names such as .aspx.
34. What are HttpHandlers?
In classic ASP.NET, an HTTP handler processes a specific HTTP request or category of requests. It implements the IHttpHandler interface or, for asynchronous processing, an appropriate asynchronous handler interface.
Example:
using System.Web;
public class HelloHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello from an HTTP handler");
}
}
Handlers are useful for processing requests for particular resources, generating dynamic content, or implementing custom request processing.
Examples of classic ASP.NET handler endpoints include WebResource.axd and ScriptResource.axd.
35. What are HttpModules?
An HTTP module participates in the ASP.NET request-processing pipeline and can respond to application events such as authentication, authorization, and error handling.
In classic ASP.NET, a module typically implements the IHttpModule interface.
using System;
using System.Web;
public class LoggingModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (sender, args) =>
{
HttpApplication app = (HttpApplication)sender;
// Perform request logging.
};
}
public void Dispose()
{
}
}
HTTP modules can be used for:
- Request logging.
- Authentication and authorization integration.
- Custom error handling.
- Request and response processing.
Difference: An HTTP handler processes a request, while an HTTP module participates in the request-processing pipeline and can observe or modify processing at different stages.
36. What are the two ways of adding constraints to a route?
In ASP.NET MVC 5, route constraints can be added using inline regular-expression constraints or by supplying constraint objects that implement IRouteConstraint.
Approach 1: Regular-expression constraint
routes.MapRoute(
name: "Employee",
url: "Employee/Details/{id}",
defaults: new
{
controller = "Employee",
action = "Details"
},
constraints: new
{
id = @"\d+"
}
);
This route matches numeric IDs such as 10 but not text values such as ABC.
Approach 2: Custom IRouteConstraint
You can create a custom class implementing IRouteConstraint and supply an instance through the route's constraints parameter.
Constraints are used to restrict which URLs match a route. They are not a replacement for validating input inside the application.
37. What are the three segments of the default route in ASP.NET MVC?
The conventional default route contains three URL segments:
{controller}/{action}/{id}
- Controller: Identifies the controller.
- Action: Identifies the action method.
- Id: An optional parameter commonly used to identify a particular record.
Example URL:
/Product/Details/25
Here, Product is the controller name, Details is the action name, and 25 is the ID.
The standard route commonly supplies default values of Home for the controller, Index for the action, and an optional ID.
38. What is an .axd file?
A file ending in .axd is commonly associated with an ASP.NET HTTP handler endpoint. In classic ASP.NET, these endpoints can generate or serve resources dynamically rather than representing ordinary physical files.
Examples include:
WebResource.axd— serves embedded resources used by ASP.NET controls.ScriptResource.axd— serves script resources used by ASP.NET AJAX and related components.Trace.axd— provides ASP.NET tracing information when enabled and configured.
The .axd extension alone does not mean that a physical file exists at that path. IIS and ASP.NET handler mappings determine how the request is processed.
39. What is BDD?
BDD stands for Behavior-Driven Development. It is a software development approach that describes application behavior using examples understandable to developers, testers, and business stakeholders.
BDD commonly uses a Given-When-Then format:
- Given: The initial context or condition.
- When: The action or event that occurs.
- Then: The expected outcome.
Example:
Feature: User login
Scenario: Login with valid credentials
Given the user has a registered account
When the user enters valid credentials
Then the user should be logged in successfully
BDD helps clarify requirements, improve collaboration, and create executable acceptance tests where supported by the chosen tools.
40. What is the advantage of using ASP.NET routing?
ASP.NET routing provides several advantages:
- Readable URLs: URLs can describe resources or actions clearly.
- SEO support: Meaningful URLs can help search engines and users understand page content.
- Separation from physical files: URLs do not have to correspond directly to physical files.
- Flexible URL design: Routes can contain parameters and optional segments.
- Centralized URL generation: MVC helpers can generate URLs based on route definitions.
- Maintainability: URL patterns can often be changed without changing every link manually.
For example, a route can expose /Products/Details/10 rather than a URL that reveals a physical file path.
| ASP-NET Interview Questions-21 to 30 :👈 | 👉:ASP-NET Interview Questions 41 to 50 |