While MVC applications typically use C# and HTML, they also introduce their own syntax and coding conventions, which are dependent on the specific technology and templating engine used.
Note: This style guide inherits rules from the C# Style Guide, C-Based Languages Style Guide, SGML-Based Languages, HTML5, and the Global Style Guide.
- Use Dependency Injection
- Use site-specific
ControllerFactory(e.g.,/ControllerFactory.cs) as composition root - Consider using an Inversion of Control (IoC) container for finding controllers by conventions
- For sites with few controllers, manually establishing dependencies is preferred
- Use site-specific
- Use out-of-the-box conventions for file locations
- Store controllers in
/Controllersfolder - Store views in
/Viewsfolder
- Store controllers in
- Only store site-specific view models in
/Models- All other models should be e.g. entity objects in separate application assembly
- Break site layout into multiple partial views
- Establish
LayoutControllerfor managing partial views - Consider a
/Layout/folder with_Layout.cshtml,Menu.cshtml,Footer.cshtml, etc. - Consider treating each view as a "component" in the SASS structure
- Establish
- Prefer specialized view models for reusable partial views
- This allows partial views to be parameterized
- This also helps standardize the model despite disparate parent views
- View model classes should be suffixed with
ViewModel(e.g.,NavigationViewModel) - View models should simply be data transfer objects (DTOs); they should not have methods
- Properties on view models should be read only; all values should be assigned via the constructor
- Strongly type views when practical
- Prefer Razor parsing engine for views
- Consider explicitly defining
@{ Layout }property for clarity - Suffix markup sections with "Section" (e.g.,
@RenderSection("HeaderSection"))- Common meta sections include:
Head,Scripts - Common sections include:
HeaderSection,TitleSection,PageBodySection,FooterSection
- Common meta sections include:
- Prefer using
@helperor@functionsover multiline inline code blocks- Prefer helpers for code responsible for outputting markup
- Follow Ignia's C# Style Guide for these
- Use
@Html.Partial()to alias views (i.e., by using onecsthtmlfor two view names)
- Each Controller should use constructor injection
- This will include e.g. data repositories (say,
IUserRepository,IPostRepository)
- This will include e.g. data repositories (say,
- If the contents of a page are not dynamic, or vary based on limited parameters, consider using
OutputCacheto improve performance - Consider using (attribute routing](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#attribute-routing) for sites with complex routing rules
- This makes it easier to understand how each action maps to the URL conventions
- Use (or extend) the ASP.NET Identity libraries for authentication (including single-sign-on), roles-based authorization, and, where practical, profile management
- If a custom database is necessary, it is preferred to write providers for the ASP.NET Identity libraries than to create a completely novel system