| by Achyut Kendre | No comments

Action Parameters and Return Values

ASP NET MVC 5

In this article we will learn about action parameters and return values in details –

Links to download Source Code :https://drive.google.com/file/d/1pICbI7zaHkLvXVzad_guyNIPU0GBsVAr/view?usp=sharing and

https://drive.google.com/file/d/1Vlj-M7RCGnD45gq70gDqNGbztHJR9tWM/view?usp=sharing

Action Parameters in ASP.NET MVC

Action can have primitive or complex types of parameters. If action has a primitive types of parameter’s by default it will search the values of those parameters in url (not compulsory) and if the parameters are of complex type it will search it in request body.

if your route is configured as follows –

routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}", 
  defaults: new { controller="Home",action= "Index",    id=UrlParameter.Optional} 
    );

You can pass one value in action parameter i.e. id using url e.g. http://localhost:89898/test/sayhello/ganesh and http://localhost:89898/test/saybye/12

Here ganesh will be loaded in id and will be available in action parameter value.

public class TestController : Controller
    {
        public string SayHello(string id)
        {
            return "Value of id is:" + id ;
        }
}
 public class TestController : Controller
    {
        public string SayBye(int id=0)
        {
            return "Value of id is:" + id;
        }
}

You can also have multiple parameters as follows –

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{str}", //id will become compulsory
                defaults: new { controller = "Home", action = "Index",id=UrlParameter.Optional,str=UrlParameter.Optional}
            );

So Now we can pass two parameters in action –

 public class TestController : Controller
    {
        public string SayHello(string id,string str)
        {
            return "Value of id is:" + id + " value of str is:" +str;
        }
}

By chance if your action has parameters those are not listed in url then we can pass them as a query string parameters

http://localhost:89898/test/doaddition/?a=10&b=20

 public class TestController : Controller
 {
       public int doAddition(int a, int b)
        {
            return a + b;
        }
}

Action Return Values in ASP.NET MVC

Action can return a primitive types or special type of result called Action result or sub types of action result called viewresult, partialviewresult etc.

The ActionResult class is a base class of all the above result classes, so it can be the return type of action method that returns any result listed above. However, you can specify the appropriate result class as a return type of action method, method along with other methods that return a particular type of result, as shown in the below table.

Result ClassDescriptionBase Controller Method
ViewResultRepresents HTML and markup.View()
EmptyResultRepresents No response.
ContentResultRepresents string literal.Content()
FileResult,Represents the content of a file.File()
JsonResultRepresents JSON that can be used in AJAX.Json()
RedirectResultRepresents a redirection to a new URL.Redirect()
RedirectToRouteResultRepresents redirection to another route.RedirectToRoute()
PartialViewResultRepresents the partial view result.PartialView()

Action Selectors in ASP.NET MVC

Action Selectors are the built in attributes those can applied on action, those will help the route engine to decide which action of which controller to invoked when user makes the request in directly we can say it will guide the route engine in the selection process of action.
[ActionName] :- This action selector will help you to specify the another name to the action, if action name is not suitable then you can define another name to the action.
[NonAction] :- This attribute will tell the roue engine that this is not a action method , so that method can not be called by route engine.

 [NonAction]  // normal method it's not action
public string SayHello()
{
return "Say Hello";
}

[ActionName("Cal")]
public string CalculateValueByBinaryComplexNumberAlgorithm()
{
return "Calculated Value";
}

How to use HttpVerbs in ASP.NET Core?

These are the built in attributes those will guide the route engine regarding selection of action as per Http method used to make request.
[HttpGet] :- when you decorate the action using HttpGet that action can be invoked only when user makes http get type of request.
[HttpPost] :- when you decorate the action using HttpPost that action can be invoked only when user makes http post type of request.
[HttpPut] :- when you decorate the action using HttpPut that action can be invoked only when user makes http put type of request.
[HttpDelete]:- when you decorate the action using HttpDelete that action can be invoked only when user makes http delete type of request.

 [HttpPost]
public string SayBye()
{
return "Say Bye 1 Called!";
}

[HttpGet]
public string SayBye(string id)
{
return "Say Bye 2 Called!";
}

In above code when you make a direct request to SayBye , by default request type will be get so it will call the say bye 2 will be get called. If a action do not have any action selector , then you can call that action using any method.

Leave a Reply