我想将我的 ASP.NET MVC Controller 操作命名为 123games
但由于数字原因它不允许我这样做。
我需要使用ActionNameAttribute
吗?
最佳答案
是的,你知道。 C# language specification禁止以数字开头的标识符,如 section 2.4.2, "Identifiers" :
identifier-start-character:
- letter-character
- _ (the underscore character U+005F)
请注意缺少任何数值。
也就是说,您可以使用 ActionNameAttribute
拥有一个带有以数字开头的操作的路由(因为 URL 没有这样的限制),如下所示:
[ActionName("123games")]
public ActionResult _123Games()
{
// Action code.
}
请注意,使用下划线对于标识符的第一个字符有效(根据上面所示的同一部分,特别是下面,强调我的):
The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex 15, except that underscore is allowed as an initial character (as is traditional in the C programming language)...
因此,我使用它作为方法名称的开头(即 _123Games
),以使其在 C# 代码中与 URL 片段保持紧密一致。
关于.net - 如何在 ASP.NET MVC 中创建以数字开头的操作名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14523866/