Site icon T4Tutorials.com

Action methods and controllers MCQs ASP.NET

What is the purpose of a controller in ASP.NET MVC?
a. To handle user input and interactions
b. To manage the database
c. To render the view
d. To define the layout

Answer: a. To handle user input and interactions

What attribute is used to define an action method in a controller?
a. [Action]
b. [Method]
c. [HttpGet]
d. [Route]

Answer: c. [HttpGet]

How do you specify that an action method should respond to HTTP POST requests?
a. [HttpPost]
b. [HttpMethod]
c. [Post]
d. [HttpRequest]

Answer: a. [HttpPost]

What does the [NonAction] attribute do?
a. Marks a method as a non-action method
b. Marks a method as an action method
c. Specifies that a method should not be executed
d. Hides the method from the view

Answer: a. Marks a method as a non-action method

How do you pass parameters to an action method in ASP.NET MVC?
a. Using query strings
b. Using route data
c. Using form data
d. All of the above

Answer: d. All of the above

Which method in a controller is used to return a view?
a. View()
b. Render()
c. Execute()
d. Show()

Answer: a. View()

How do you return a JSON result from an action method?
a. Json()
b. ReturnJson()
c. JsonResult()
d. JsonData()

Answer: c. JsonResult()

What is the purpose of the [Route] attribute?
a. To define the route URL pattern
b. To specify the HTTP method
c. To return a view
d. To handle errors

Answer: a. To define the route URL pattern

How do you return a file from an action method?
a. File()
b. ReturnFile()
c. DownloadFile()
d. SendFile()

Answer: a. File()

What is the default return type of an action method in ASP.NET MVC?
a. ActionResult
b. JsonResult
c. ViewResult
d. FileResult

Answer: a. ActionResult

How do you redirect to another action method in the same controller?
a. Redirect()
b. RedirectToAction()
c. GoToAction()
d. MoveToAction()

Answer: b. RedirectToAction()

What method is used to render a partial view from an action method?
a. PartialView()
b. RenderPartial()
c. ViewPartial()
d. Partial()

Answer: a. PartialView()

How do you access the query string parameters in an action method?
a. Using Request.QueryString
b. Using Request.Params
c. Using HttpContext.Current.Request.QueryString
d. Using RouteData.Values

Answer: a. Using Request.QueryString

What attribute is used to handle HTTP PUT requests in an action method?
a. [HttpPut]
b. [HttpMethod]
c. [Put]
d. [HttpRequest]

Answer: a. [HttpPut]

How can you make an action method asynchronous?
a. By using the async keyword and returning Task<ActionResult>
b. By using the async keyword and returning ActionResult
c. By using Task<ActionResult> without async
d. By using await keyword only

Answer: a. By using the async keyword and returning Task<ActionResult>

What does the [HttpDelete] attribute specify?
a. That an action method should handle HTTP DELETE requests
b. That an action method should handle HTTP GET requests
c. That an action method should handle HTTP POST requests
d. That an action method should handle HTTP PUT requests

Answer: a. That an action method should handle HTTP DELETE requests

How do you handle exceptions in an action method?
a. Using try-catch blocks
b. Using the [Exception] attribute
c. Using global error handlers only
d. Using custom error pages

Answer: a. Using try-catch blocks

Which method in a controller allows you to execute custom logic before or after an action method runs?
a. OnActionExecuting()
b. OnActionExecuted()
c. OnAction()
d. OnExecute()

Answer: a. OnActionExecuting()

How do you pass data from a controller to a view?
a. Using ViewBag
b. Using ViewData
c. Using TempData
d. All of the above

Answer: d. All of the above

What does the [AcceptVerbs] attribute do?
a. Specifies the HTTP methods that an action method can handle
b. Defines the URL route pattern for an action method
c. Renders the view
d. Handles exceptions

Answer: a. Specifies the HTTP methods that an action method can handle

How do you access route data in an action method?
a. Using RouteData.Values
b. Using Request.RouteValues
c. Using HttpContext.Current.RouteData
d. Using RouteContext.Values

Answer: a. Using RouteData.Values

Which method is used to return a status code from an action method?
a. StatusCode()
b. HttpStatusCode()
c. HttpResponse()
d. HttpResponseMessage()

Answer: a. StatusCode()

How do you create a new instance of a controller in ASP.NET MVC?
a. By using the ControllerFactory
b. By creating a new object with new keyword
c. By using Dependency Injection
d. By using the Create() method

Answer: c. By using Dependency Injection

What method allows you to render a view without a layout in ASP.NET MVC?
a. View(viewName, model, false)
b. View(viewName, model)
c. PartialView(viewName, model)
d. RenderView(viewName, model)

Answer: a. View(viewName, model, false)

How do you implement a default action method in a controller?
a. By defining a method named Index
b. By using the [DefaultAction] attribute
c. By specifying a default route in RouteConfig
d. By overriding the Execute method

Answer: a. By defining a method named Index

Which method is used to set the value of ViewBag in a controller action?
a. ViewBag.PropertyName = value
b. ViewData[“PropertyName”] = value
c. TempData[“PropertyName”] = value
d. Model.PropertyName = value

Answer: a. ViewBag.PropertyName = value

How can you access HTTP headers in an action method?
a. Using Request.Headers
b. Using HttpContext.Current.Request.Headers
c. Using HttpHeaders
d. Using Request.Params

Answer: a. Using Request.Headers

What attribute is used to ensure an action method only handles HTTPS requests?
a. [RequireHttps]
b. [Secure]
c. [HttpsOnly]
d. [RequireSecure]

Answer: a. [RequireHttps]

How do you specify the name of the action method in the URL for routing?
a. By using the Route attribute
b. By using the RouteConfig class
c. By using the URL parameter
d. By using the RouteData.Values

Answer: a. By using the Route attribute

Which method in a controller is used to return a view with a model?
a. View(model)
b. ViewResult(model)
c. PartialView(model)
d. RenderView(model)

Answer: a. View(model)

What is the purpose of the [Authorize] attribute in ASP.NET MVC?
a. To restrict access to an action method based on user roles
b. To log user actions
c. To validate user input
d. To handle exceptions

Answer: a. To restrict access to an action method based on user roles

How do you set the content type of a file being returned from an action method?
a. By specifying contentType in the File() method
b. By setting the ContentType property of HttpResponse
c. By setting the content type in the FileResult object
d. By using Response.ContentType

Answer: b. By setting the ContentType property of HttpResponse

What is the purpose of the [RoutePrefix] attribute?
a. To define a common route prefix for all action methods in a controller
b. To specify the route for a single action method
c. To define the default route pattern
d. To set the route constraints

Answer: a. To define a common route prefix for all action methods in a controller

How do you define a custom route in ASP.NET MVC?
a. By adding a route to RouteConfig
b. By using the [Route] attribute on an action method
c. By modifying the web.config file
d. By using the RouteBuilder class

Answer: a. By adding a route to RouteConfig

What method in a controller is used to return a result indicating that an operation was successful?
a. Ok()
b. Success()
c. Status()
d. Return()

Answer: a. Ok()

How do you handle model binding errors in an action method?
a. By checking ModelState.IsValid
b. By using the [Bind] attribute
c. By handling exceptions
d. By using the [ValidateModel] attribute

Answer: a. By checking ModelState.IsValid

What attribute is used to specify that an action method should be executed only when a request is made with a specific HTTP method?
a. [HttpMethod]
b. [HttpRequest]
c. [Http]
d. [HttpGet]

Answer: d. [HttpGet]

How do you ensure that an action method is only accessible to authenticated users?
a. By using the [Authorize] attribute
b. By using the [Authenticated] attribute
c. By using the [Security] attribute
d. By using the [LoginRequired] attribute

Answer: a. By using the [Authorize] attribute

What is the purpose of the [ValidateAntiForgeryToken] attribute?
a. To prevent cross-site request forgery (CSRF) attacks
b. To validate the model state
c. To handle authentication
d. To validate user input

Answer: a. To prevent cross-site request forgery (CSRF) attacks

How do you access form data in an action method?
a. Using Request.Form
b. Using Request.QueryString
c. Using RouteData.Values
d. Using ViewData

Answer: a. Using Request.Form

What method in a controller is used to return an HTTP status code result?
a. HttpStatusCodeResult()
b. StatusCodeResult()
c. HttpResult()
d. StatusResult()

Answer: a. HttpStatusCodeResult()

How do you pass data between actions within the same controller?
a. By using TempData
b. By using ViewBag
c. By using ViewData
d. By using RouteData

Answer: a. By using TempData

Which attribute is used to define a route constraint for an action method?
a. [Route]
b. [Constraint]
c. [RouteConstraint]
d. [Require]

Answer: a. [Route]

How do you return a view that uses a different layout from the default layout in an action method?
a. By specifying a different layout in the View() method
b. By using the Layout property in the View() method
c. By creating a new layout file
d. By using a different ViewResult

Answer: b. By using the Layout property in the View() method

What does the [AllowAnonymous] attribute do?
a. Allows access to an action method without authentication
b. Restricts access to an action method based on roles
c. Requires authentication for an action method
d. Specifies the action method’s route

Answer: a. Allows access to an action method without authentication

Exit mobile version