asp.net-mvc ActionName 的用途
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6536559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Purpose of ActionName
提问by Hasan Fahim
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?
使用“ActionName”属性为操作方法设置别名有什么好处?我真的没有看到它有多大好处,为用户提供调用具有其他名称的操作方法的选项。指定别名后,用户只能使用别名调用操作方法。但是,如果这是必需的,那么为什么用户不更改操作方法的名称而不是为其指定别名呢?
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.
如果有人可以为我提供一个在可以提供巨大好处或最好使用的场景中使用“ActionName”的示例,我将不胜感激。
回答by Buildstarted
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)
它允许您以数字开始您的操作或包含任何 .net 不允许在标识符中的字符。- 最常见的原因是它允许您有两个具有相同签名的操作(请参阅任何脚手架控制器的 GET/POST 删除操作)
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
.
例如:您可以在 url 操作名称http://example.com/products/create-product
vshttp://example.com/products/createproduct
或http://example.com/products/create_product
.
public class ProductsController {
[ActionName("create-product")]
public ActionResult CreateProduct() {
return View();
}
}
回答by Carlos Mu?oz
It is also useful if you have two Actions with the same signature that should have the same url.
如果您有两个具有相同签名且应该具有相同 url 的 Action,这也很有用。
A simple example:
一个简单的例子:
public ActionResult SomeAction()
{
...
}
[ActionName("SomeAction")]
[HttpPost]
public ActionResult SomeActionPost()
{
...
}
回答by RHicke
I use it when the user downloads a report so that they can open their csv file directly into Excel easily.
我在用户下载报告时使用它,以便他们可以轻松地将 csv 文件直接打开到 Excel 中。
[ActionName("GetCSV.csv")]
public ActionResult GetCSV(){
string csv = CreateCSV();
return new ContentResult() { Content = csv, ContentEncoding = System.Text.Encoding.UTF8, ContentType = "text/csv" };
}
回答by Chary
Try this code:
试试这个代码:
public class ProductsController
{
[ActionName("create-product")]
public ActionResult CreateProduct()
{
return View("CreateProduct");
}
}
回答by Pradeep Yadav
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.
此类表示用于操作名称的属性。它还允许开发人员使用与方法名称不同的操作名称。
回答by Hrishikesh T T
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.
这里一个 ActorView 不接受任何参数,另一个接受 int。第一种方法用于查看演员列表,另一种用于删除 ID 为“id”的项目后显示相同的演员列表。您可以在任何需要方法重载的地方使用动作名称作为“ActorViewOverload”。