88

What's the benefit of setting an alias for an action method using the "ActionName" attribute? I really don't see much benefit of it, in providing the user the option to call an action method with some other name. After specifying the alias, the user is able to call the action method only using the alias. But if that is required then why doesn't the user change the name of the action method rather then specifying an alias for it?

I would really appreciate if anyone can provide me an example of the use of "ActionName" in a scenario where it can provide great benefit or it is best to use.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Hasan Fahim
  • 3,875
  • 1
  • 30
  • 51
  • The most common reason is when you have both GET and POST methods and the signature is the same. see @Carlos Muñoz answer – RickAndMSFT May 10 '16 at 02:57

6 Answers6

139

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

For example: you could allow dashes within your url action name 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();
    }

}
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • 48
    It also lets you use action names that might be methods on `Controller`, like View or File. – gram Jun 30 '11 at 15:02
  • @gram, can't we call a normal method on a controller without specifying an alias? I think we can. – Hasan Fahim Jun 30 '11 at 15:22
  • 2
    If you wanted to name your `Action` `View()` then you'd have problems because all references to `View()` in your current controller would resolve to that `Action` rather than the underlying base method. So to get around this you would use the `ActionName` attribute to allow for the `View` action but internally you'd call it `PublicView` or something similar. – Buildstarted Jun 30 '11 at 15:27
  • 3
    I think you'll have to `return View("CreateProduct")` or .NET will nag about not finding a view such as `create-product.aspx` or `create-product.cshtml` -- At least my code works like this. – Achilles Jun 17 '13 at 12:34
  • @gram So how do you do that? Obviously not with an attribute. – John Nov 05 '14 at 08:41
62

It is also useful if you have two Actions with the same signature that should have the same url.

A simple example:

public ActionResult SomeAction()
{
    ...
}

[ActionName("SomeAction")]
[HttpPost]
public ActionResult SomeActionPost()
{
    ...
}
Pinch
  • 4,009
  • 8
  • 40
  • 60
Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80
  • Makes sense, but in what scenarios do you use HttpPost without parameters? I know there are possible reasons, like returning JSON and avoiding security issues with GET. I'm just wondering what yours is. – regularmike May 23 '14 at 13:34
  • 2
    The parameter list is not the important part here. There might be a better example, the point is that you can do it if you need to. – Carlos Muñoz May 23 '14 at 17:27
  • 1
    we can do that by replacing "SomeActionPost" to "SomeAction" also, then what is the use of ActionName?? – Jilani pasha Mar 05 '18 at 11:09
  • You can't name both methods the same if they have the same parameters. In this case [ActionName] renames the action (not the method) as registered in ASP. NET MVC route table to the intended one. – Carlos Muñoz Mar 05 '18 at 14:32
39

I use it when the user downloads a report so that they can open their csv file directly into Excel easily.

[ActionName("GetCSV.csv")]
public ActionResult GetCSV(){
    string csv = CreateCSV();
    return new ContentResult() { Content = csv, ContentEncoding = System.Text.Encoding.UTF8, ContentType = "text/csv" };
}
RHicke
  • 3,494
  • 3
  • 23
  • 23
2

Try this code:

public class ProductsController
 {

    [ActionName("create-product")]
    public ActionResult CreateProduct() 
    {
        return View("CreateProduct");
    }

}
STF
  • 1,485
  • 3
  • 19
  • 36
Chary
  • 21
  • 1
1

It is also helpful 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 accepts no parameters and the other accepts int. The first method used for viewing actor list and the other one is used for showing the same actor list after deleting an item with ID as 'id'. You can use action name as 'ActorViewOverload' whereever you need method overloading.

Hrishikesh T T
  • 339
  • 2
  • 11
1

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

Pradeep Yadav
  • 63
  • 1
  • 1
  • 8