Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an action name that starts with a number in ASP.NET MVC?

I want to name my ASP.NET MVC controller action as 123games but it is not allowing me because of numeric numbers.

Do I need to use ActionNameAttribute?

like image 449
DotnetSparrow Avatar asked Jan 25 '13 14:01

DotnetSparrow


1 Answers

Yes, you do. The C# language specification forbids identifiers that begin with a number, as per section 2.4.2, "Identifiers":

identifier-start-character:

  • letter-character
  • _ (the underscore character U+005F)

Note the lack of any numeric value.

That said, you can use the ActionNameAttribute to have a route with an action that begins with a number (because URLs have no such restriction), like so:

[ActionName("123games")]
public ActionResult _123Games()
{
     // Action code.
}

Note that using an underscore is valid for the first character of an identifier (as per the same section as seen above, and specifically below, emphasis mine):

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)...

So I've used that as the beginning of your method name (i.e. _123Games) to keep it as closely aligned in your C# code as it is with the URL fragment.

like image 179
casperOne Avatar answered Sep 22 '22 01:09

casperOne