MCSD Developing ASP.NET MVC Web Applications 2 — Questions and Answers
Question 1: What is Action Filter in ASP.NET MVC?
- A Razor helper method
- An attribute that injects logic before or after an action executes (Correct answer)
- A routing constraint
- A model validation rule
Correct answer: An attribute that injects logic before or after an action executes
Action filters are attributes that run custom code before or after an action method executes.
Question 2: Which interface do you implement to create a custom model binder in ASP.NET MVC?
- IModelMetadataProvider
- IModelBinder (Correct answer)
- IActionResult
- IValueProvider
Correct answer: IModelBinder
Custom model binders implement `IModelBinder` to control how HTTP request data is mapped to action parameters.
Question 3: What does `TempData` do in ASP.NET MVC?
- Persists data across multiple requests indefinitely
- Stores data for the current and the next request only (Correct answer)
- Caches data server-side for all users
- Shares data between controllers
Correct answer: Stores data for the current and the next request only
TempData stores data that persists only until it is read, typically across a single redirect.
Question 4: In ASP.NET MVC, what does the `RedirectToAction()` method return?
- ViewResult
- ContentResult
- RedirectToRouteResult (Correct answer)
- JsonResult
Correct answer: RedirectToRouteResult
`RedirectToAction()` returns a `RedirectToRouteResult` that issues an HTTP redirect to another action.
Question 5: Which file in an ASP.NET MVC project defines the default routing?
- Web.config
- Global.asax / RouteConfig.cs (Correct answer)
- Startup.cs
- ActionFilters.cs
Correct answer: Global.asax / RouteConfig.cs
Route configuration is typically registered in `RouteConfig.cs` and called from `Global.asax.cs`.
Question 6: What is the purpose of `Html.BeginForm()` in a Razor view?
- Creates an AJAX form
- Renders an opening HTML form tag that posts to a controller action (Correct answer)
- Validates form inputs client-side
- Generates anti-forgery tokens only
Correct answer: Renders an opening HTML form tag that posts to a controller action
`Html.BeginForm()` generates an opening `<form>` tag that submits to the specified or default controller action.
What is Action Filter in ASP.NET MVC?