ActionName 的用途 [英] Purpose of ActionName

查看:46
本文介绍了ActionName 的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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?

如果有人能向我提供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.

推荐答案

它允许您以数字开头或包含任何 .net 不允许在标识符中的字符. -最常见的原因是它允许您有两个具有相同签名的操作(请参阅任何脚手架控制器的 GET/POST 删除操作)

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)

例如:您可以在 url 操作名称中允许破折号 http://example.com/products/create-producthttp://example.com/products/createproducthttp://example.com/products/create_product.

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

}

这篇关于ActionName 的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆