Handling Singular and Plural Controllers/Routes

后端 未结 5 2005
忘了有多久
忘了有多久 2021-01-31 09:12

I\'m a little confused on how I should handle singular and plural routes and controllers in my web application.

The website is a simple quotes site - think Einstein, Sha

5条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 09:37

    Just because the default controllers of asp-mvc have singular names, it doesn't mean you should implement singular form for all your controllers.

    The correct answer is: it depends on the quantity of the entity that your controller represents.

    Singular, example the AccountController is singular because it represents actions (action method) pertaining to a single account only.

    Plural If your controller contains at least one action method that handles multiple entities at a single transaction.

    Example plural format

    users/update/3
    

    The route above makes you think you are editing all users, which does not makes sense if you read it like a sentence. However, if you read your route like query, it will make much more sense.

    If we think about it, a route IS a query: {entities}/{action}/{parameter} looks like a query to me.

    users/ shorthand of users/all reads "select all users table"

    users/123 reads "select single entity from users table"

    users/update/123 reads "update single entity from users table"

    Major sites use plural format, see sample below

    stackoverflow.com/questions          <- list of questions   (multiple)
    stackoverflow.com/questions/18570158 <- individual question (single)
    stackoverflow.com/questions/ask      <- new question        (single)
    
    stackoverflow.com/users        <- display list of users (multple)
    stackoverflow.com/users/114403 <- individual user       (single)
    
    asp.net/mvc/tutorials        <- display list of tutorials (multiple) 
    asp.net/mvc/tutorials/mvc-5  <- individual tutorial       (single)
    
    facebook.com/messages/     <- Display list of messages (multiple)
    facebook.com/messages/new  <- Create a single message  (single)
    facebook.com/messages/john <- view individual messages (multiple)
    

    I believe that English grammar should be strictly incorporated in every programming aspect. It reads more natural, and leads to good code hygiene.

提交回复
热议问题