Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of ActionName

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.

like image 212
Hasan Fahim Avatar asked Jun 30 '11 14:06

Hasan Fahim


People also ask

What is the use of ActionName attribute?

ActionName attribute is an action selector which is used for a different name of the action method. We use ActionName attribute when we want that action method to be called with a different name instead of the actual name of the method.

What is the use of non action method in MVC?

Net MVC C#? The NonAction attribute is used when we want a public method in a controller but do not want to treat it as an action method. An action method is a public method in a controller that can be invoked using a URL.

What are the action selectors?

Action selectors are attributes that can be applied to action methods and are used to influence which action method gets invoked in response to a request. It helps the routing engine to select the correct action method to handle a particular request.

What is an action name?

An action name is a name that you use to describe user-initiated events corresponding to the action parameter in grecaptcha.


3 Answers

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();
    }

}
like image 152
Buildstarted Avatar answered Oct 21 '22 10:10

Buildstarted


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()
{
    ...
}
like image 62
Carlos Muñoz Avatar answered Oct 21 '22 11:10

Carlos Muñoz


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" };
}
like image 39
RHicke Avatar answered Oct 21 '22 11:10

RHicke