Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The Action Methods in ASP.NET MVC are responsible for executing requests and responding to them. The response is generated by default as an ActionResult. User interactions and actions correspond to one-to-one in most cases.
The Controller class contains all public methods referred to as Action methods. These methods follow the standard rules with the following modifications:
The action method must be public. You cannot keep it private or protected
Overloading an action method is not allowed
It is not possible to have a static method for an action method.
In the MVC Controller, all public methods are action methods. By decorating the action method with the "NonAction" attribute, we can make the public method non-action. This restricts the action method's rendering on the browser.
For example, the below code illustrates the Index() action method from the NewController class.
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace ActionMethod {
public class NewController: Controller {
public ActionResult Index() {
return View();
}
}
}
The Index() method of the View() method returns the ActionResult using the public property, as you can see from the code given above.
How to call Action Method?
The Syntax for calling an Action Method:
YourDomainName/ControllerName/ActionMethodName
Here is an example:
localhost/Home/Index
ActionResult
The MVC framework supports several Result classes that can be returned from action methods. Various response types are represented by result classes, including HTML, files, strings, JSON, and javascript. ASP.NET MVC provides some result classes.
The following table lists them all.
Action Result
Method
ViewResult
View()
EmptyResult
None
RedirectResult
Redirect()
JsonResult
Json()
JavaScriptResult
Javascript()
ContentResult
Content()
FileContentResult
FilePathResult
FileStreamResult
File()
PartialViewResult
PartialView()
All of the above result classes can be returned by a method that returns ActionResult since this class is the base class for all of them. The action method returns a specific result class, but you may also specify the appropriate result class.
Types of Action Methods
ViewResult
It is a representation of HTML and markup. The index page will be returned in the following code.
public ViewResult Index() {
ViewList.List = "List of values in view";
return View();
}
EmptyResult
An empty page with no result will be returned if this action method returns a null result.
public EmptyResult Index() {
ViewList.List = "List of values in view";
return new EmptyResult();
}
RedirectResult
Results can be redirected to a specific URL by using Redirect Results. RedirectResult Action Method is useful when redirecting to another action method. It will direct you to the Details Page.
public RedirectResult Index() {
return Redirect("Home/Details");
}
JsonResult
A JSON Result file consists of key-value pairs and a simple text file format. In some circumstances, you may wish to return data in JSON format. In that manner, JSONResult is your best choice.
public JsonResult Index() {
Example ex = new Example() {
Name = "Ninja1",
Id = "00001"
};
return Json(ex, JsonRequestBehavior.AllowGet);
}
public class Example {
public string Name {
get;
set;
}
public string Id {
get;
set;
}
}
JavaScriptResult
This method returns a javascript that can be executed on the client browser. The browser receives javascript content in response.
JavaScript can be executed on the client at run time using this block below.
Add this JavaScriptResult() method in home controller.
[HttpGet]
public JavaScriptResult WarningMessage() {
var message = "alert('Click ok if you want to continue?');";
return new JavaScriptResult() {
Script = message
};
}
The user-defined content type is returned. A plain text message can be sent to the browser screen using this function below.
using System;
using System.Web.Mvc;
using System.Collections.Generic;
Namespace MvcProject {
public class HomeController: Controller {
[HttpGet]
public ContentResult Index() {
return Content("What's up developers", "text/plain", System.Text.Encoding.UTF8);
}
}
}
FileContentResult
The downloadable file represents the binary content.
@Html.ActionLink("Download Text File","Download","Home")
This file contains the content of the file.
FilePathResult
It contains a path to a downloadable file.
FileStreamResult
It represents a downloadable file in the form of a file stream.
PartialViewResult
Returns HTML from Partial view.
In HomeController.cs add the following code.
[HttpGet]
public PartialViewResult messagepage() {
return PartialView("message");
}
Now, in Index.cshtml add this code.
@{
Html.RenderAction("messagepage", "Home");
}
FAQs
How do ActionResult and ViewResult differ? ViewResult is a subtype of ActionResult, which is its base type. You can use ActionResult as the return type for action and any subtypes within this type, e.g., JSON, PartialView, View, RedirectToAction. If you use subtypes, as in this case ViewResult, you are binding your action so it can only return subtypes as results, which is View.
When to Use ViewResult? The ViewResult property can be used if you know that your action method will return a page. Nevertheless, your action method may behave differently, such as rendering a view or performing a redirection. The return type of this method can be the more general ActionResult class.
Is it possible to have the same action name in MVC? It is possible to have two actions with the same name in ASP.NET MVC. You will not be able to use the same name and parameters in two methods on .NET. If you want ASP.NET MVC to know they are one action, you'll need to rename the methods using the ActionName attribute.
What is the child action method in MVC? An MVC Child Action is what it sounds like. Like ASP.NET web forms, ASP.NET MVC Child Actions are like User Controls. Controllers can execute for an area within the rendered area of a view, just like UserControls in Web Forms can perform for an area within the rendered area of a page.
Key Takeaways
This blog shows that responses are generated by ASP.NET MVC Action Methods, which execute requests. ActionResult is the default response type.
There are different types of action results that are returned by actions. They are all subclasses of the ActionResult class.
Try the guided path from Coding Ninjas if you are a beginner who is interested in learning topics of Web Development and Data Structures and Algorithms.