[ASP.NET MVC] Project Structure and Conventions

December 27, 2011 at 4:01 pm Leave a comment

In ASP.NET MVC, “Convention over Configuration” concept is one of the most distinguishing features. Rather than saving all settings in the configuration files, MVC heavily relies on the common agreement (convention) such as folder structures and naming conventions.

1. Project Structure

When you create an MVC project using one of MVC project templates in VS, you will see the common structure of folders and files. The structure of files and folders are not just for your convenience. It follows the predefined rules. Therefore  developers should understand well how folders and files are organized.

 

2. Folders

It is OK to create custom folders wherever you want. But some folders play important roles on their own.

  • /bin :  for the compiled assemblies along with any referenced assemblies that are not in the GAC
  • /Content : for static content such as CSS files and images
  • /Controllers :  for the controller classes
  • /Models :  for view model and domain model classes
  • /Scripts : for JavaScript libraries. jQuery and Microsoft AJAX helpers are in this folder by default
  • /Views : for views and partial views, usually grouped together in folders named after the controller
  • /Views/Shared : for layouts and views which are not specific to a single controller

VS Solution explorer hides the “/bin” folder unless the “Show All Files” button is clicked.

Actually it is OK to put content files, script files, model classes and controllers (unlike the views) anywhere in the project. But by doing so, you are breaking the convention and will confuse other developers who look at your code. Please stick to the convention!!!

 

3. Files

  • /Web.config : main configuration file for the application
  • /Views/Web.config : contains the configuration required to make views work with ASP.NET and prevents views from being directly served by IIS
  • /Global.asax : the global ASP.NET application class. Its code-behind class (Global.asax.cs) is the place to register routing configuration, as well as handle application-level events such as Application_Start.

Note that MVC project templates create 2 Web.config files. The config file in the “/Views” folder does the important task: blocking direct access to view files.

<handlers>
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

When you access any view page directly, you will get an 404 (Not Found) error. That’s what the “BlockViewHandler” does for the all files in the “Views” folder.

 

4. Special Folders

There are some special folders that are not created by a project template. You can create these folders whenever you wish.

  • /Areas : Areas are the way of partitioning a large application into smaller pieces
  • /App_Data : for private data, such as XML files or file-based databases such as SQL Server Express
  • /App_GlobalResources : for resources (.resx and .resources files) that are compiled into assemblies with global scope
  • /App_LocalResources : for resources (.resx and .resources files) that are associated with a specific page or a user control
  • /App_Browsers : for browser definitions (.browser files) that ASP.NET uses to identify individual browsers and determine their capabilities
  • /App_Themes : for a collection of files (.skin, .css files, and image files) that define the appearance of pages and controls

The “/Areas” folder is specific to MVC but other folders are used for Web Forms Applications.

 

5. MVC Conventions – Suggested

  • 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

 

6. MVC Conventions – Required

  • Controller classes must have names that end with “Controller” such as “ProductController
  • Views and partial views should go into the folder “/Views/Controllername” or “/Views/Shared
  • The default view for an action method should be named after that method. (for example, the “List” action method is linked to the “List.cshtml” view file.)
  • The naming convention for layouts is to prefix the file with an underscore (_) character and layout files are placed in the /Views/Shared” folder.
  • HTML form fields should have the same name as Model Properties for correct data binding.

The Layout file in MVC is like a Master file in Web forms. In a view file, you need to specify the “Layout” property.

@{
  Layout = "~/Views/Shared/MyLayout.cshtml";
}
@{
  Layout = null;
}
About these ads

Entry filed under: ASP.NET, MVC. Tags: , .

[ASP.NET MVC] Views – View Engines [ASP.NET MVC] Routing – Outgoing URLs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


About

As a web developer, I am happy to have a place to share my little know-hows regarding web-development.

MCSD

Recent Posts

Categories


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: