Brian O'Neil
Brian O'Neil

Reputation: 21

Why does ASP.NET MVC rename controllers that start with a number?

I tried to create a Controller class with the name 1BigController, but MVC won't let me do that. It changes the controller name to _BigController.

Why can't I start a controller name with a number?

Upvotes: 2

Views: 1101

Answers (4)

Marcus Shockley
Marcus Shockley

Reputation: 310

This is a convention of all C# classes, not just MVC. You can find more in the C# Language Specification (item 2.4.2)

Here's a similar question regarding variable names and numeric vals

Why can't variable names start with numbers?

Upvotes: 5

Peter Olson
Peter Olson

Reputation: 142911

Variable names have to start with a letter or underscore. The remaining characters can be letters, underscores, or digits. I am not sure why this is the convention, but it has been for a long time.

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61589

MVC uses a convention approach to type resolution. A controller name such as "Big" will cause MVC to search for a BigController. Likewise, if it was not sanitised as "_" and instead was "1Big", MVC would attempt to find a type called 1BigController. Type names cannot start with numbers, so it gets sanitised for you.

Upvotes: 0

Young Grasshopper
Young Grasshopper

Reputation: 354

It could be because you can't have class names that start with a number. I used to program PLC's and the software we used wouldn't allow us to start any tag names with a number. I don't know why we couldn't other then that was the rule.

Upvotes: 1

Related Questions