ASP VS MVC
ASP VS MVC
ASP VS MVC
Answer: ASP.NET Web Forms follows an event-driven model, where the UI is tightly
coupled with the business logic and the state management is handled by server-side controls.
In contrast, ASP.NET MVC follows a model-view-controller pattern that promotes a clear
separation of concerns.
For example, in Web Forms, page lifecycle events control the flow of the application. In
MVC, the request is processed by a controller, which then interacts with the model and
selects a view to render.
Answer: Routing in ASP.NET MVC maps incoming requests to specific controller actions
based on URL patterns defined in the RouteConfig class.
For example:
csharp
Copy code
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
In this example, a request like /Product/Details/5 would be routed to the Details action
of the ProductController, passing 5 as the id.
Answer: Controllers are responsible for handling user input, processing requests, and
returning responses. They act as an intermediary between the Model and the View.
For example:
csharp
Copy code
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
In this code, the Index action method processes the request and returns the Index view to the
user.
For example:
csharp
Copy code
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Using a ViewModel ensures that the view only gets the necessary data, promoting a clean
separation between the business logic and presentation layers.
Answer: Model binding in ASP.NET MVC is the process of converting incoming request
data (e.g., form fields) into .NET objects. It automatically maps the request data to action
method parameters or model properties.
For example:
csharp
Copy code
public ActionResult Create(ProductViewModel model)
{
if (ModelState.IsValid)
{
// Save the model to the database
}
return View(model);
}
In this action, if a user submits a form with fields matching the ProductViewModel
properties, MVC will automatically populate the model parameter.
Answer: Action filters are attributes that can be applied to controller actions or entire
controllers to execute code before or after an action method runs. They are used for tasks like
logging, authentication, and authorization.
For example:
csharp
Copy code
[Authorize]
public class AdminController : Controller
{
public ActionResult Dashboard()
{
return View();
}
}
In this example, the Authorize filter ensures that only authenticated users can access the
Dashboard action.
Answer:
ViewBag: A dynamic object that allows you to pass data from the controller to the
view. It is not strongly typed.
ViewData: A dictionary that stores data as key-value pairs and also allows passing
data to the view but is not strongly typed.
TempData: Used to store data temporarily (for one request) and is ideal for passing
data between actions. It uses session storage.
Example:
csharp
Copy code
ViewBag.Message = "Hello, World!";
ViewData["Message"] = "Hello, World!";
TempData["Message"] = "Hello, World!";
Answer: Razor is a view engine used in ASP.NET MVC for generating dynamic HTML
content. It allows developers to mix HTML with C# code seamlessly, providing a clean and
intuitive syntax.
Example:
razor
Copy code
@model ProductViewModel
<h1>@Model.Name</h1>
<p>Price: @Model.Price</p>
In this example, Razor syntax (@Model.Name) allows accessing the model properties directly
within the HTML markup.
For example:
csharp
Copy code
public class ProductViewModel
{
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
When this model is used in a form, MVC will automatically generate the necessary
JavaScript for client-side validation based on the annotations.
Answer: Areas in ASP.NET MVC are used to partition large applications into smaller, more
manageable sections. Each area can have its own set of controllers, views, and routes, helping
to organize the application better.
markdown
Copy code
/Areas
/Admin
/Controllers
/Views
/Models
This structure allows you to manage administrative features separately from the main
application, enhancing maintainability and clarity.