MCSD Developing ASP.NET MVC Web Applications 1 — Questions and Answers
Question 1: In ASP.NET MVC, which method is used to return a view from a controller action?
- RedirectToAction()
- Content()
- View() (Correct answer)
- Json()
Correct answer: View()
The `View()` method creates a `ViewResult` that renders an HTML response using a Razor view.
Question 2: What is the role of a Model in the MVC design pattern?
- Handles HTTP routing
- Represents the data and business logic (Correct answer)
- Renders the HTML output
- Processes middleware requests
Correct answer: Represents the data and business logic
The Model in MVC represents the application's data, business rules, and logic.
Question 3: Which attribute in ASP.NET MVC restricts an action to HTTP POST requests only?
- [HttpGet]
- [HttpPost] (Correct answer)
- [Authorize]
- [Route]
Correct answer: [HttpPost]
The `[HttpPost]` attribute limits the action method to respond only to HTTP POST requests.
Question 4: What does the `@model` directive do in a Razor view?
- Defines a layout page
- Specifies the type of the model passed to the view (Correct answer)
- Imports a namespace
- Declares a partial view
Correct answer: Specifies the type of the model passed to the view
The `@model` directive declares the type of the strongly-typed model that the view expects.
Question 5: In ASP.NET MVC, what is ViewBag?
- A strongly typed model container
- A dynamic property bag for passing data from controller to view (Correct answer)
- A session state object
- A caching mechanism
Correct answer: A dynamic property bag for passing data from controller to view
ViewBag is a dynamic object that allows controllers to pass arbitrary data to views without a strongly-typed model.
Question 6: What is the purpose of the `[ValidateAntiForgeryToken]` attribute in ASP.NET MVC?
- Validates model data annotations
- Protects against Cross-Site Request Forgery (CSRF) attacks (Correct answer)
- Checks user authentication
- Enforces HTTPS
Correct answer: Protects against Cross-Site Request Forgery (CSRF) attacks
The `[ValidateAntiForgeryToken]` attribute validates a hidden token to protect POST actions from CSRF attacks.
In ASP.NET MVC, which method is used to return a view from a controller action?