Conventions of ASP.NET MVC

By | November 11, 2014

->. There are two types of conventions in an MVC project.

i). Just suggestions.

=>. Suggestions for “how to structure the project”.

=>. In ASP.NET MVC some suggestions are

–>Put content files (css and image files) to the “/Content”  folder

–>Put script files to the “/Script”  folder

–>Put model classes to the “/Models”  folder

–>Put controller classes to the “/Controllers”  folder

=>. So other MVC developers would easily find them

=>. Also NuGet packages will install there.

=>. But this convention is not mandatory for example we can place Model classes into another folder even another project and will work fine if we refer them properly.

=>. One can rename these folders, or remove them entirely and put those contents somewhere else.

=>. That would not prevent the MVC Framework from running application because all those elements will refer from other locations.

 

ii). Convention over configuration:

According to Wikipedia

Convention over configuration (also known as coding by convention) is a software design paradigm which seeks to

=>.Decrease the number of decisions that developers need to make,

=>. Gaining simplicity, but not necessarily losing flexibility.

The phrase essentially means a “developer only needs to specify unconventional aspects of the application.”

->.So we can simply explain it

=> Rather than writing a configuration for things one just has to follow the conventions

=> So, things are “magically” picked up.

->. For example one doesn’t need to explicitly configure associations between controllers and their views. Just follow the naming convention for View and Controller so everything works fine.

->. The following sections explain the conventions that are used in place of configuration:

a). Conventions for Controller Classes:

#. Controller classes must have names that end with Controller i.e. each controller class must have Suffix Controller.

#.For example :

Controller Name Prefix Suffix
ProductController Product Controller
AdminController Admin
HomeController Home

#. When we refer any Controller from anywhere in the application we’ll just refer it by its prefix i.e. Instead of specify Full name we’ll just refer it by its prefix.

 #.For example if we want to refer HomeController from HTML helper method then we’ll just refer it by its prefix (Home) i.e. instead of specifying full name (HomeController) we’ll just specify prefix (Home).

 #. The MVC framework will internally take care of the rest i.e. appends Controller to the name and starts looking for the controller class.

#.Uses of standard names in controller actions will give us following facilities:

=>. Makes code easier to read for other developers and in future self.

=>.It also saves you time so that you can prevent endless debates about the “right” way to name something.

#.Stephen Walther suggested “Standard Controller Action Names” in his blog as follows:

Action Sample URL Description
Details /Product/Details/5 Displays a single resource such as a database record. For example, displays a single Product with an Id of 5.
Index /Product/Index Displays a collection of resources. For example, displays all of the products in the products database table.
Create /Product/Create Displays a form for creating a new resource. For example, displays a form for creating a new product.
Insert /Product/Insert Inserts a new resource into the database. Typically, you redirect to another action after performing an Insert.
Edit /Product/Edit/5 Displays a form for editing an existing resource. For example, displays a form for editing a product with an Id of 5.
Update /Product/Update/5 Updates existing resources in the database. Typically, you redirect to another action after performing an Update.
Destroy /Product/Destroy/5 Displays a page that confirms whether or not you want to delete a resource from the database.
Delete /Product/Delete/5 Deletes a resource from the database. Typically, you redirect to another action after performing a Delete.
Login /Home/Login Displays a login form.
Logout /Home/Logout Logs out a user. Typically, you redirect to another action after performing a Logout.
Authenticate /Home/Authenticate Authenticates a user name and password. Typically, you redirect to another action after performing an Authenticate.

b). Conventions for Views:

b.1). Location of Views:

#. All Views will placed inside “Views/ControllerName” folder

#. Example: All “Views” that associated with HomeController will placed inside Views/Home folder here Home is the name of Controller (As discussed step a “When referred any Controller not need to specify suffix of that controller, only specify prefix, MVC framework will internally take care).

b.2). Name of the View:

#. There is no restriction to naming a view i.e. one can name his/her view whatever he/she like.

#. But recommended to

=>. Name the view as same as the Action Method name of associated controller.

=>.Especially for a default view of controller action, view name should be the same as the action method name

=>. So that the parameter less overload of View() method can return default view associated with action method”.

=>.For example the default view associated with an action method called Index should be called Index.cshtml.

=>. So, for the Index action method in the HomeController class, the default view is will be /Views/Home/Index.cshtml.

=>. The default view is used when you return the result of calling the View method in an action method, like this:

return View();

=> But if we use view name other than controllers action method name e.g. List.cshtml for Index() action method then we have to return view with parameter ViewName as follows:

Return View(“List”);

Note:

->. I do not include the file name extension or the path to the view.

->. When looking for a view, the MVC Framework looks inside “/Views/folder that’s name match with that Controller name i.e. /Views/ControllerName folder” and if not found then inside “/Views/Shared folder”.

->. This means that I can put views that will be used by more than one controller in the /Views/Shared folder and the framework will find them.

c). Conventions for Layouts:

#. Layouts name should be with prefix underscore (_) character e.g. _Layout.cshtml.

#.Layouts files are placed inside “/Views/Shared folder”.

#Default layout should apply to all views by default through “/Views/_ViewStart.cshtml” file.

#. If one doesn’t want the default layout applied to views, can change the settings in _ViewStart.cshtml (or delete the file entirely) to specify another layout in the view as follows:

@{

        Layout = “~/Views/Shared/_MyLayout.cshtml”;

}

Or can disable any layout for a given view, like this:

@{

         Layout = null;

}

 

707 Total Views 1 Views Today
Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
https://www.scrumalliance.org/community/profile/mhaque13

Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Category: Asp.net MVC Tags: , , ,
Md. Mojammel Haque

About Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org) Certification Profile Link- https://www.scrumalliance.org/community/profile/mhaque13 Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

3 thoughts on “Conventions of ASP.NET MVC

Leave a Reply

Your email address will not be published. Required fields are marked *