| by Achyut Kendre | No comments

How to use Action Parameters, Return Values & Action Selectors in ASP.NET Core?

ASP.NET Core 3.1
Action Parameters and Return Values

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

Action Parameters

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.

Let us understand it by example .

Action Return Values in ASP.Net Core

Action can return a primitive types of values or a special type of result called ActionResult or sub types of ActionResults like ViewResult, PartialViewResult, RedirectResult etc.

Action Selectors in ASP.NET Core

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.