"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

Creating an Action (C#)

by Microsoft

Learn how to add a new action to an ASP.NET MVC controller. Learn about the requirements for a method to be an action.

The goal of this tutorial is to explain how you can create a new controller action. You learn about the requirements of an action method. You also learn how to prevent a method from being exposed as an action.

Adding an Action to a Controller

You add a new action to a controller by adding a new method to the controller. For example, the controller in Listing 1 contains an action named Index() and an action named SayHello(). Both methods are exposed as actions.

Listing 1 - Controllers.cs

[!code-csharpMain]

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.Mvc;
   6:   
   7:  namespace MvcApplication1.Controllers
   8:  {
   9:      [HandleError]
  10:      public class HomeController : Controller
  11:      {
  12:          public ActionResult Index()
  13:          {
  14:              return View();
  15:          }
  16:   
  17:          public string SayHello()
  18:          {
  19:              return "Hello!";
  20:          }
  21:      }
  22:  }

In order to be exposed to the universe as an action, a method must meet certain requirements:

Notice that there are no restrictions on the return type of a controller action. A controller action can return a string, a DateTime, an instance of the Random class, or void. 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.

When you add any method that does not violate these requirements to a controller, the method is exposed as a controller action. Be careful here. A controller action can be invoked by anyone connected to the Internet. Do not, for example, create a DeleteMyWebsite() controller action.

Preventing a Public Method from Being Invoked

If you need to create a public method in a controller class and you don’t want to expose the method as a controller action then you can prevent the method from being invoked by using the [NonAction] attribute. For example, the controller in Listing 2 contains a public method named CompanySecrets() that is decorated with the [NonAction] attribute.

Listing 2 - Controllers.cs

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      public class WorkController : Controller
   6:      {
   7:          [NonAction]
   8:          public string CompanySecrets()
   9:          {
  10:              return "This information is secret.";
  11:          }
  12:      }
  13:  }

If you attempt to invoke the CompanySecrets() controller action by typing /Work/CompanySecrets into the address bar of your browser then you’ll get the error message in Figure 1.

Invoking a NonAction method

Figure 01: Invoking a NonAction method(Click to view full-size image)

Previous Next





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions-1/controllers-and-routing/creating-an-action-cs.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>