0

I want to know which is the best convention for controller, action and view naming in ASP.Net MVC

When i create a "Product" Controller, should i name it productController or ProductController ? By default, visual studio Create ProductController. But that means the url will be http://mywebsite/Product/xxxx

Or we should not have upper case like this in URLs ? So i do not know which convention apply. This is the same for view names and action names...

testpresta
  • 429
  • 5
  • 15
  • 2
    Uppercase or lowercase doesn't matter in url path (for windows servers). For classes convention is usually like ProductController, starting with uppercase. For url pattern I would use custom route attributes and you can configure routing in RouteConfig also. – mehmet mecek Dec 11 '15 at 10:49
  • For upper or lowercase in url, you can check this answer: http://stackoverflow.com/a/13511455/2524010 – mehmet mecek Dec 11 '15 at 10:53
  • The conventions of MVC are the conventions of C#/VB. Modifying the name of something like a class to serve the purposes of a URL, is a mistake. Write your classes like classes should be written and then worry about things like the URL after the fact. – Chris Pratt Dec 11 '15 at 14:11

2 Answers2

4

you can use First latter capital as default MVC does, and for lower case url you can use RouteCollection.LowercaseUrls Property

check below link https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.lowercaseurls.aspx

Example:

public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
}
Rohit
  • 135
  • 1
  • 2
  • 8
3

Controller is just a class and should follow class capitalization rules, iow should use PascalCase: https://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx

You can use StyleCop to check your code for such things as casing rules, spaces, etc: https://stylecop.codeplex.com/

But still nobody can prevent you from naming like 'productController'.

Also, if you wish to change appearance of controller name in URL, please check next question and corresponding answers (but usually people ten to use default naming): How to Change ASP.NET MVC Controller Name in URL?

In short, you can do this with RoutePrefixAttribute or by configuring routing (not shown here):

[RoutePrefix("product")]
public class ProductController
{
    ...
}
Community
  • 1
  • 1