Target ActionName

What is the advantage of setting an alias for an action method using the "ActionName" attribute? I really do not see much benefit in this, giving the user the opportunity to call an action method with some other name. After specifying an alias, a user can only call an action method using an alias. But if this is required, why does the user not change the name of the action method, and then specify an alias for it?

I would really appreciate it if someone could provide me with an example of using "ActionName" in a scenario where it can be very useful or better used.

+74
asp.net-mvc asp.net-mvc-3
Jun 30 2018-11-11T00:
source share
8 answers

This allows you to start the action with a number or include any character that .net does not allow in the identifier. - The most common reason is that it allows you to have two actions with the same signature (see GET / POST Delete actions of any forest controller)

For example: you can allow dashes inside the name of your URL http://example.com/products/create-product vs http://example.com/products/createproduct or http://example.com/products/create_product .

 public class ProductsController { [ActionName("create-product")] public ActionResult CreateProduct() { return View(); } } 
+119
Jun 30 '11 at 2:59 p.m.
source share

This is also useful if you have two actions with the same signature that must have the same URL.

A simple example:

 public ActionResult SomeAction() { ... } [ActionName("SomeAction")] [HttpPost] public ActionResult SomeActionPost() { ... } 
+55
Jun 30 2018-11-11T00:
source share

I use it when a user loads a report so that he can easily open his csv file directly in Excel.

 [ActionName("GetCSV.csv")] public ActionResult GetCSV(){ string csv = CreateCSV(); return new ContentResult() { Content = csv, ContentEncoding = System.Text.Encoding.UTF8, ContentType = "text/csv" }; } 
+31
May 22 '12 at 18:31
source share

The ActionName attributes are used to map .NET action methods with different names to the outside world. In the circumstances below, you can use the ActionName attributes:

  • For SEO (Search Engine Optimization), you want to show a more meaningful action name in the URL than the name of the .NET action method.

  • To prevent overloading features related to problems. Suppose you have a DELETE action method name whose GET and POST methods accept the same parameter. In this case, you will get a compile time error. To solve the problem, you can rename your .NET method and the ActionName attribute, you can still support the same name.

For a more detailed explanation, visit the ActionName attribute in ASP.NET MVC

+6
Jun 03 '15 at 8:55
source share

Try this code:

 public class ProductsController { [ActionName("create-product")] public ActionResult CreateProduct() { return View("CreateProduct"); } } 
+2
Apr 19 '17 at 5:29 on
source share

This class represents the attribute that is used for the name of the action. It also allows developers to use a different action name than the method name.

+1
May 26 '17 at 8:10
source share

This is also useful when you need to implement method overloading.

  public ActionResult ActorView() { return View(actorsList); } [ActionName("ActorViewOverload")] public ActionResult ActorView(int id) { return RedirectToAction("ActorView","Home"); } ` 

Here, one ActorView takes no parameters, and the other takes an int. The first method, used to view the list of participants and another, is used to display the same list of participants after deleting the element with the id id. You can use the action name as "ActorViewOverload" if you need method overloading.

0
Nov 26 '15 at 7:16
source share

Action selectors: An action selector is an attribute that can be applied to action methods. This helps the routing mechanism select the right action method to process a specific request. MVC 5 includes the following action selector attributes:

  • Actionname
  • Inaction
  • Action verbs

ActionName: The ActionName attribute allows us to specify an action name other than the method name. Consider the following example.

 public class StudentController : Controller { //1. [ActionName("find")] public ActionResult GetById(int id) { return View("GetById"); } //2. [ActionName("BadRequest")] public ActionResult Error() { return View("Error"); } //3. [ActionName("students-list")] public ActionResult StudentsList() { return View("StudentsList"); } } 

[ActionName] has many advantages, such as:

  1. You can customize the name of your method and give it any name of your choice, as in the example above.
  2. You can use this attribute if you want to hide your real method name from the user, as described above.
  3. Visual Studio throws an error when you use hyphen "-" in the method name, using lowercase letters will say "Naming rule violation" and so on. In such cases, this attribute helps a lot.

Note. The View () method by default looks for a suitable method / action name, so we pass the real method name as a parameter when the view returns.

0
Jan 23 '19 at 9:22
source share



All Articles