articles

Home / DeveloperSection / Articles / Actions in ASP.NET MVC 3

Actions in ASP.NET MVC 3

Vijay Shukla 5474 24-Oct-2012

ActionResult:

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views and also redirect to another controller's Action method. ActionResult is an abstract class. Every ActionResult Classes are derived from "ActionResult". The Controller actions will return the ActionResult object.

         public ActionResult Index ()
                    {
                         return View();
                    }

  [AcitionName] attribute:-

it’s allows you to start your action with a number or include any character that .NET does not allow in an identifier. - This is probably the only real reason it exists.

   [ActionName("NewAbout")]

        public ActionResult About()
        {
            return Content("Hello from New About", "text/plain");
        }     

Content:-

When we require returning any text from a Controller action, we will apply the Content type(it’s use when we don’t have any View in our MVC3 Application).

Content have three parameters:-

 Content(“string Content”, ”string Content Type”, ”Unicode Text Format”);

   public ActionResult About()
        {
            return Content("Hello from New About", "text/plain",System.Text.Encoding.UTF8);
        }

     

Actions in ASP.NET MVC 3

RedirectToAction:-

RedirectToAction method is use for redirect any other action or Controller. For use RedirectToAction method we need Two Controller Home Controller and Sample Controller.

That is Home Controller:-
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult Verify()
        {
            return RedirectToAction("Verify", "Sample");
        }
     }
That is Sample Controller:-


    public class SampleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }         public ActionResult Verify()
        {
            return Content("Hello from Verify action in Sample Controller");
        }
    }
That is Home/ Index.cshtml :-
@{ViewBag.Title = "Index";}

<h2> Home’s Index</h2>
@ViewBag.message
<a href="/Home/Verify">Click for call Verify Action</a>


Actions in ASP.NET MVC 3

When click on Click for call Verify Action then Control goes on the Home’s Verify Action


Now this line wills Execute from Home Controller Verify Action Method-


    return RedirectToAction("Verify", "Sample");

 

RedirectToAction() method will call the Sample Controller’s Verify Action Method.

 

Actions in ASP.NET MVC 3

Finally this screen will appear.

Routers:-

Routing is the technique of construct significant URLs for a web request. MVC

applications URLs are not represented with extensions name like .aspx(in earlier


version). Instead, the URLs consist of the Controller name and Action name.


In Global.asax.cs file we can see the Application_Start() and RegisterRoutes()


methods for Default routing works.



        public static void RegisterRoutes(RouteCollection routes)

        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                } // Parameter defaults
            );
        }

 

In this above declaration where we map the routing Our URL pattern is


{controller}/{action}/{id}". Here id is optional Parameter

.

new{ controller = "Home", action = "Index", id = UrlParameter.Optional} it is set the


default parameter for Routing. In the above line first parameter for Controller name


,second parameter is for Action Name and third parameter for the id.

Controllers:-

Now we can create two Controllers HomeController and SampleController in our application for Routing.

Right Click on the controller folder which is available in solution explorer and Add new controller with the name of SampleController.


    public class SampleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Verify()
        {
            return Content("Hello from Verify action in Sample Controller");
        }
    }

In this  SampleController  don’t have any View so here is need to return some text  from our Action then use the Content class.

 
Index.cshtml :-

  Now goto to the Home’s Index.cshtml View and write that code for


@{ViewBag.Title = "Index";}
<h2> Home’s Index</h2>
@ViewBag.message
<a href="/Sample/Verify">Click for call Verify Action</a>

 

 Now Run the project with ctrl+F5. And click the link.


Actions in ASP.NET MVC 3


After Clicking the This Screen will appear.


Actions in ASP.NET MVC 3

 

Now when you delete the “/Verify” in the URL Bar and press Enter key. After deletion this screen will appear.

Actions in ASP.NET MVC 3

Now Goto in the Global.asax.cs file and write that code in the


RegisterRoutes(RouteCollection routes) method.


routes.MapRoute(

         "Sample",
         "Sample/{action}",
         new
         {
        controller = "Sample",
          action = "Verify",
        id = UrlParameter.Optional
     });

 

And after write that code you can Run your project delete “/Verify” and press enter


now you will see that output. 


Actions in ASP.NET MVC 3



Updated 07-Sep-2019

Leave Comment

Comments

Liked By