Action Methods in MVC

We can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream. The ASP.NET MVC framework will convert any return type that is not an action result into a string and render the string to the browser. Create a simple controller as in the following code snippet:

public class HomeController : Controller

{

    public ActionResult SayHello()

    {

        ViewData[“SayHello”] = “Hello”;

        return View();

    }

    public string SayHi()

    {

        return “hi from ‘SayHi’.”;

    }

}

We must create a view to run SayHello() since it returns the View as viewshome SayHello.cshtml. To execute second action method we don’t need to create a view. It will render the string in the browser. We can also have a void public method as an action method. So we can say any public method can be an action method.

We can make a public function a non-action by decorating it with [NonAction] as in the following:

[NonAction]

public string SayHi()

{

   return “hi from ‘SayHi’.”;

}

Getting Results

The successful execution of an MVC controller action will produce an object derived from ActionResult. Rendering a view and redirecting the browser to a new URL are both valid types of results we can get from our controller.

 

Overloading

 

MVC supports the method overloading but it doesn’t support method overloading based solely on signature, so this will fail.

 

 

public string SayHello()

{

    return “Hello from ‘SayHello’.”;

}

public string SayHello(string name)

{

    return “Hello ” + name + ” from ‘SayHello’ “;

}

 

 

We need to decorate our action method with two attributes, like [HpptGet] and [HttpPost]. We can use the ActionName attribute to expose two methods with the same name as actions with different names.

public class HomeController : Controller

{

    [ActionName(“SayHelloWithOutName”)]

    public string SayHello()

    {

        return “Hello from ‘SayHello(SayHelloWithOutName)’.”;

    }

    public string SayHello(string name)

    {

        return “Hello ” + name + ” from ‘SayHello’ “;

    }

    public string SayHi()

    {

        return “hi from ‘SayHi’.”;

    }

}

We can create our own ActionFilter derived from ActionMethodSelectorAttribute. Here we are checking the URL parameter then invoke the appropriate method.

public class MyActionSelectorAttribute : ActionMethodSelectorAttribute

{

    public MyActionSelectorAttribute(string valueName)

    {

        ValueName = valueName;

    }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)

    {

        return (controllerContext.HttpContext.Request[ValueName] != null);

    }

    public string ValueName { get; private set; }

}

We need to decorate our method with the parameter MyActionSelectorAttribute as in the following code snippet:

public string SayHello()

{

    return “Hello from ‘SayHello’.”;

}

[MyActionSelectorAttribute(“name”)]

public string SayHello(string name)

{

     return “Hello ” + name + ” from ‘SayHello’ “;

}

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *