Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ASP VS MVC

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. What is the primary difference between ASP.

NET Web Forms and


ASP.NET 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.

2. How does routing work in ASP.NET MVC?

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.

3. Explain the role of controllers in ASP.NET MVC.

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.

4. What is a ViewModel, and why is it used in ASP.NET MVC?


Answer: A ViewModel is a class that contains data specifically tailored for a view. It is used
to pass data from the controller to the view, encapsulating only the data needed for that view.

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.

5. How does model binding work in ASP.NET MVC?

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.

6. What are Action Filters in ASP.NET MVC?

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.

7. Describe the differences between ViewBag, ViewData, and TempData.

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!";

8. What is the purpose of Razor in ASP.NET MVC?

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.

9. How do you implement client-side validation in ASP.NET MVC?

Answer: Client-side validation in ASP.NET MVC can be implemented using Data


Annotations in the model and leveraging unobtrusive JavaScript.

For example:

csharp
Copy code
public class ProductViewModel
{
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }

[Range(1, 1000, ErrorMessage = "Price must be between 1 and 1000.")]


public decimal Price { 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.

10. Explain the concept of Areas in ASP.NET MVC.

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.

For example, if you have an Admin area:

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.

You might also like