TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Tutorial

ASP.Net MVC Actions with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET Controller Actions and Parameters

In ASP.NET MVC application, the controller defines action methods that are used to handle user requests and render view as the response. A controller can have any number of actions.

A user request can be any of like: entering URL into the browser, clicking a link or submitting a form.

The MVC application uses the routing rules that are defined in the Global.asax.cs file. This file is used to parse the URL and determine the path of the controller. Now, controller executes the appropriate action to handle the user request.


ActionResult Return Type

The ActionResult class is the base class for all action results. Action methods return an instance of this class. There can be different action result types depending on the task that the action is implementing. For example, if an action is to call the View method, the View method returns an instance of the ViewResult which is derived from the ActionResult class.

We can also create action method that returns any type of object like: integer, string etc.

The following table contains built-in action result types.

Action Result Helper Method Description
ViewResult View It is used to render a view as a Web page.
PartialViewResult PartialView It is used to render a partial view.
RedirectResult Redirect It is used to redirect to another action method by using its URL.
RedirectToRouteResult RedirectToAction RedirectToRoute It is used to redirect to another action method.
ContentResult Content It is used to return a user-defined content type.
JsonResult Json It is used to return a serialized JSON object.
JavaScriptResult JavaScript It is used to return a script that can be executed on the client.
FileResult File It is used to return binary output to write to the response.
EmptyResult (None) It represents a return value that is used if the action method must return a null result.

Adding an Action method

Here, we will add a new action method to the controller that we crested in previous chapter.

To add action to the existing controller, we need to define a public method to our controller. Our MusicStoreController.cs file is looks like the following after adding a welcome action method.

// MusicStoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class MusicStoreController : Controller
    {
        // GET: MusicStrore
        public ActionResult Index()
        {
            return View();
        }
        public string Welcome()
        {
            return "Hello, this is welcome action message";
        }
    }
}

Output:

To access the welcome action method, execute the application then access it by using MusicStore/Welcome URL. It will produce the following output.

ASP Action and parameters 1

Action Method Parameters

Action parameters are the variables that are used to retrieve user requested values from the URL.

The parameters are retrieved from the request's data collection. It includes name/value pairs for form data, query string value etc. the controller class locates for the parameters values based on the RouteData instance. If the value is present, it is passed to the parameter. Otherwise, exception is thrown.

The Controller class provides two properties Request and Response that can be used to get handle user request and response.

Example

Here, we are creating an action method in the controller. This action method has a parameter. The controller code looks like this:

// MusicStoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class MusicStoreController : Controller
    {
        // GET: MusicStrore
        public ActionResult Index()
        {
            return View();
        }
        public string ShowMusic(string MusicTitle)
        {
            return "You selected " + MusicTitle + " Music";
        }
    }
}

Output:

In URL, we have to pass the parameter value. So, we are doing it by this URL localhost:port-no/MusicStore/ShowMusic?MusicTitle=Classic. it produces the following result.

ASP Action and parameters 2




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf