In this article, I am going to discuss the Most Frequently asked ASP.NET MVC Action Methods Interview Questions and Answers. Please read our previous article where we discussed the most frequently asked Views Interview Questions and Answers in ASP.NET MVC.
What are Action methods in ASP.NET MVC?
All public methods of a Controller in ASP.NET MVC application which respond to the incoming URL are considered as Action Methods.
Controller actions are methods defined in the controller class and responsible to perform required operations on the user’s inputs like form values, query strings values, etc. with the help of Model and passing the results back to the View. Asp.net MVC has the following built-in ActionResults Type and Helper methods:
What is ActionResult and how is it different from others?
The ActionResult class is the base class for all action results. An action result can be of type ViewResult, JsonResult, RedirectResult and so on. Hence, when your action method returns multiple results based on different conditions, ActionResult is the best choice. Since it can return any type of result.
How to make a Non-Action method in ASP.NET MVC?
By default, the ASP.NET MVC framework treats all public methods of a controller class as action methods. If you do not want a public method to be an action method, you must mark that method with the NonActionAttribute attribute.
Can you change the action method name?
We can also change the action method name by using the ActionName attribute. Now action method will be called by the name defined by the ActionName attribute.
Now, DoSomething action will be identified and called by the name DoAction.
How to restrict an action method to be invoked only by HTTP GET, POST, PUT or DELETE?
By default, each and every action method can be invoked by an HTTP request (i.e. GET, PUT, POST, and DELETE). But you can restrict an action to be invoked only by a specific HTTP request by applying HttpGet or HttpPost or HttpPut or HttpDelete attribute.
If you want to restrict an action method for HTTP Get request only then decorate it with HttpGet action method selector attribute as given below:
How to determine an action method is invoked by HTTP GET or POST?
By using the HttpMethod property of HttpRequestBase class, you can find out whether an action is invoked by HTTP GET or POST.
How to determine an AJAX request?
We can determine an AJAX request by using Request.IsAjaxRequest() method. It will return true if the request is an AJAX request else returns false.
What is Child action and how to invoke it?
Child actions are useful for creating reusable widgets that could be embedded in your views. In ASP.NET MVC partial views are used to create reusable widgets and a partial can be rendered by an action method. This action method can have a child attribute and has its independent MVC lifecycle from the parent view. Also, an action that has a child’s attribute cannot be called independently. It always will be called within a parent view otherwise it would give an error.
A child action is invoked by using @Html.RenderAction or @Html.Action helper methods from inside of a view.
In the next article, I am going to discuss the most frequently asked Data Annotations Interview Questions in ASP.NET MVC Application with Answers. Here, in this article, I try to explain the most frequently asked ASP.NET MVC Action Methods Interview Questions and Answers. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.
What are Action methods in ASP.NET MVC?
All public methods of a Controller in ASP.NET MVC application which respond to the incoming URL are considered as Action Methods.
Controller actions are methods defined in the controller class and responsible to perform required operations on the user’s inputs like form values, query strings values, etc. with the help of Model and passing the results back to the View. Asp.net MVC has the following built-in ActionResults Type and Helper methods:
- ViewResult – Returns a ViewResult which renders the specified or default view by using the controller View() helper method.
- PartialViewResult – Returns a PartialViewResult which renders the specified or default partial view (means a view without its layout) by using the controller PartialView() helper method.
- RedirectResult – Returns a RedirectResult which Issues an HTTP 301 or 302 redirection to a specific URL by using controller Redirect() helper method.
- RedirectToRouteResult – Returns a RedirectToRouteResult which Issues an HTTP 301 or 302 redirection to an action method or specific route entry by using controller RedirectToAction(), RedirectToActionPermanent(), RedirectToRoute(), RedirectToRoutePermanent() helper methods.
- ContentResult – Returns a ContentResult which renders raw text like “Hello, DotNet Tutorials!” by using the controller Content() helper method.
- JsonResult – Returns a JsonResult which serializes an object in JSON format ( like as “{ “Message”: Hello, World! }”) and renders it by using controller Json() helper method.
- JavaScriptResult – Returns a JavaScriptResult which renders a snippet of JavaScript code like as “function hello() { alert(Hello, World!); }” by using controller JavaScript() helper method. This is used only in AJAX scenarios.
- FileResult – Returns a FileResult which renders the contents of a file like PDF, DOC, Excel, etc. by using the controller File() helper method.
- EmptyResult – Returns no result returned by an action. This has no controller helper method.
- HttpNotFoundResult – Returns an HttpNotFoundResult which renders a 404 HTTP Status Code response by using controller HttpNotFound() helper method.
- HttpUnauthorizedResult – Returns an HttpUnauthorizedResult which renders a 401 HTTP Status Code (means “not authorized”) response. This has no controller helper method. This is used for authentication (forms authentication or Windows authentication) to ask the user to log in.
- HttpStatusCodeResult – Returns an HttpStatusCodeResult which renders a specified HTTP code response. This has no controller helper method.
What is ActionResult and how is it different from others?
The ActionResult class is the base class for all action results. An action result can be of type ViewResult, JsonResult, RedirectResult and so on. Hence, when your action method returns multiple results based on different conditions, ActionResult is the best choice. Since it can return any type of result.
How to make a Non-Action method in ASP.NET MVC?
By default, the ASP.NET MVC framework treats all public methods of a controller class as action methods. If you do not want a public method to be an action method, you must mark that method with the NonActionAttribute attribute.
[NonAction] public void DoSomething() { // Method logic }
Can you change the action method name?
We can also change the action method name by using the ActionName attribute. Now action method will be called by the name defined by the ActionName attribute.
[ActionName("DoAction")] public ActionResult DoSomething() { //TODO: return View(); }
Now, DoSomething action will be identified and called by the name DoAction.
How to restrict an action method to be invoked only by HTTP GET, POST, PUT or DELETE?
By default, each and every action method can be invoked by an HTTP request (i.e. GET, PUT, POST, and DELETE). But you can restrict an action to be invoked only by a specific HTTP request by applying HttpGet or HttpPost or HttpPut or HttpDelete attribute.
If you want to restrict an action method for HTTP Get request only then decorate it with HttpGet action method selector attribute as given below:
[HttpGet] public ActionResult Index() { //TODO: return View(); }
How to determine an action method is invoked by HTTP GET or POST?
By using the HttpMethod property of HttpRequestBase class, you can find out whether an action is invoked by HTTP GET or POST.
public ActionResult Index(int? id) { if (Request.HttpMethod == "GET") { //TODO: } else if (Request.HttpMethod == "POST") { //TODO: } else { //TODO: } return View(); }
How to determine an AJAX request?
We can determine an AJAX request by using Request.IsAjaxRequest() method. It will return true if the request is an AJAX request else returns false.
public ActionResult DoSomething() { if (Request.IsAjaxRequest()) { //TODO: } return View(); }
What is Child action and how to invoke it?
Child actions are useful for creating reusable widgets that could be embedded in your views. In ASP.NET MVC partial views are used to create reusable widgets and a partial can be rendered by an action method. This action method can have a child attribute and has its independent MVC lifecycle from the parent view. Also, an action that has a child’s attribute cannot be called independently. It always will be called within a parent view otherwise it would give an error.
[ChildActionOnly] public ActionResult MenuBar() { //TODO: return PartialView(); }
A child action is invoked by using @Html.RenderAction or @Html.Action helper methods from inside of a view.
In the next article, I am going to discuss the most frequently asked Data Annotations Interview Questions in ASP.NET MVC Application with Answers. Here, in this article, I try to explain the most frequently asked ASP.NET MVC Action Methods Interview Questions and Answers. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.