Language for controllers names in ASP.NET MVC

泪湿孤枕 提交于 2019-12-13 19:15:21

问题


Should my controllers names be in my native language or in English? What is the current best practice? And if in English, how do I make sure that the urls stays in my native tongue?

I know that my webapp won't be multilingual and I rather not write a lot of boilerplate.


回答1:


The convention in ASP.NET MVC is that controller and action names are the same as the controller and action in the URI except in special cases, like ambiguous overloads. I think it is a good idea to follow that convention, even if it means that the controller and action names will be in your native language and the rest of the code would be in English. If I were asked to debug a web site which displayed in some language I don't understand like Swahili, I would find it considerably easier to find the appropriate controller and action for a certain page if they reflected the Swahili URI than I would if it were translated into English.

Note, however, that most Unicode characters cannot appear in a URI (without Punycode). Only a restricted subset of ASCII characters can appear in the URI. See the specification for details.




回答2:


From a SEO perspective i think its a good standard to use the same language on the controllers/views/actions that your MVC site is based on.

In my case i have a few sites based on my native language swedish, to get best search engine optimization and page ranking i'm using the same language when i name my controllers/views/action as the content language on the sites.

Inside controllers/views/actions I always use english as coding and commenting language.

I don't know if this is the best approach, but it feels like it is a good pattern to follow if you care about search engine optimization and page ranking.




回答3:


You can write controller and controller's method names in any language (in UTF-8), that's not a problem. For a website which supports only 1 language that should be fine.




回答4:


It's your app so of course the choice is yours, but if you have native language URIs then I'd also go for native language controller & action names for consistency and ease of use for you.

Jeff Atwood wrote an interesting post on the topic of software development and the English language (not sure I agree but it is interesting and relevant). It won't answer your question but it's worth a read.




回答5:


If you're going to have an international team look at your code then have English names. If you're going to work on it all by yourself, or have local people who speak languages then you can have names in your local languages.

Remember though, naming conventions are geared towards English and not for local languages. So that's a problem.




回答6:


My opinion is maybe to try to load your routes definitions explicitly per language in your global.asax.

const string DEFAULT_LANGUAGE = "en";

routes.MapRoute("Product_EN", "en/Product/{action}", new { controller="Product", action="Index"} );

routes.MapRoute("Product_FR", "fr/Produit/{action}", new { controller="Product", action="Index"} );

routes.MapRoute("Product_ES", "es/Produto/{action}", new { controller="Product", action="Index"} );

route.MapRoute("Default", "{language}/{controller}/{action}/{id}", new { language = DEFAULT_LANGUAGE, controller="Home", action="Index", id=""});

Note: These are example, you should retreive the translated names from global resources.

But then we are forced to refer the correct route name in your views to use RouteActions. Anyway, it is a bit work-around solution but allows you to have translated URLs into your website like:

www.mysite.com/produit/afficher www.mysite.com/product/show ...

I hope it helps, Cheers

Fred.



来源:https://stackoverflow.com/questions/725220/language-for-controllers-names-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!