Tuesday, 10 June 2014

ASP.NET MVC 4 - Part [2] - Project Items


Table Of Contents

Introduction

ASP.NET MVC relies on Convention over Configuration. If you want to make your website development easy and productive it's important to understand the Project Items or Application Folder Structure of an ASP.NET MVC Application. In this article I will explain the Project Items of an Internet Application Template of ASP.NET MVC 4. If you are new to MVC then you might want to check my previous article on Introduction to MVC 4.
I hope you will enjoy this article.I have put my time and efforts on all of my articles, Please don't forget to mark yourvotessuggestions and feedback to improve the quality of this and upcoming articles.

Overview – Project Items

While creating a new application, as of now there are six different Project Templates available with ASP.NET MVC 4 . If you have not yet explored all the project templates with MVC 4, please check my MVC 4 - Introduction article.
All project templates gives a common structure, some of the templates are having some different files and or folders for specific purpose. The below given screen-shot is of Internet Application Template of MVC 4.
It’s very important to know the structure of application and file/folder conventions. In this article I’m going to explain all the above project items from a beginner’s perspective. For getting a clear picture I have added more files and folders to the newly created solution. Some of the folders are not exactly useful for ASP.NET MVC but still I have added that folders too so that a new comer also can understand which is used for what. Lets explore the Files/Folders from the above Solution (InternetMVC4App).
Note: The attached files doesn't have any functionalities implemented, it's just the application used for this article.

Solution File

Solution is a grouping of one or more projects that work together to create an application. In a simple way we can say a solution is a structure for organizing projects in Visual Studio.
Solution File
Read more from MSDN.

Project File

The project file is an XML document that contains all the information and instructions that MSBuild needs in-order to build your project, like the contents to include, the platform requirements, versioning information, web server or database server settings, and the tasks that must be performed. Every Visual Studio project includes an MSBuild project file, with a file extension that reflects the type of project—for example, a C# project (.csproj), a Visual Basic.NET project (.vbproj), or a database project (.dbproj).
Read more from www.asp.net.

Properties Folder

When talking about the "Properties" Folder, the first thing I wanted to mention is the AppDesignerFolder entry in project file in our case .csproj file.

Properties folder is the first folder in the project node. If you expand the "Properties" Folder, you can see a file namedAssemblyInfo.cs.
The AssemblyInfo.cs contains the details related to assembly version information and other information about the assembly that will be displayed in detail section of properties window of the assembly. The content of AssemblyInfo.cs is given below,
The below given screenshot shows the assembly version details in the 'properties' window of assembly file.

If you double-click on this folder then it will show Project Properties page instead of expanding it. Click here to view larger image

If required, the properties folder name can be renamed but we need to update the .csproj file accordingly. If you are deleting the file AssemblyInfo.cs or folder Properties then the assembly details won’t be available in the details tab. The below given screenshot shows the assembly version details after deleting the folder or file.
Assembly Properties, No Version Number
The above image shows default values for assembly details.

Reference Folder

These are assemblies that have been referenced in our current project. If you want to add any assembly reference or any service (WCF Web Service) reference you can add it from here.
To add an assembly reference just right click on the Reference folder and click on the 'Add Reference' menu, please refer the below screenshot.
Once you click on the above (Add Reference) menu then a popup will be shown like below screen, select the required assembly and click on OK.
Add Assembly Reference
If you wanted to add a service reference then click on 'Add Service' menu, once you clicked on 'Add Service ' menu, you will get a screen a screen like below where you can give the service URL,
give the service URL ( you can give .svc or .asmx URL here) if you want to get the old Add Web Service menu then click on 'Advanced' option there you can see the 'Add Web Reference' menu.

After clicking on 'Add Web Reference' menu , now you got the old Web Service screen where you used to give.asmx URL.
Web Service Reference

App_Browsers Folder

App_Browsers folder contains browser definitions ( .browser files). Browser definition files contain definitions for individual browsers. At run time, ASP.NET uses the information in the request header to determine what type of browser has made the request. Then ASP.NET uses .browser files to determine the capabilities of the browser.
Browser definition files were introduced in .NET Framework version 2.0. In earlier versions of the .NET Framework, thebrowserCaps element was used to define browser definitions in configuration files.
You can find existing browser definition files at
%SystemRoot%\Microsoft.NET\Framework\[VERSION]\Config\Browsers\
here [VERSION] is the framework version that you have already installed.
If you want to change the definitions of any of these browser definition files, Just copy the appropriate file to our project's App_Browsers folder and modify the definition as per the requirement. The below screen shot shows the content of newly added BrowserFile1.browser file, We can see that it's just an XML file that describes the capabilities of the browser.
Browser Definition File Sample
Read more on Browser Definition File from MSDN.

App_Code Folder

App_Code is used for keeping class files , typed objects etc. Code from the App_Code folder is referenced automatically in our application.The App_Code folder can contain subdirectories of files, which can include class files that in different programming languages (eg:- .cs,.vb).
As per my understanding App_Code is not really meant for Web Application Projects, if we are using App_Code in WAP (Web Application Project) then we need to change the "Build Action" to 'Content' from 'Compile' to work everything as expected, Read more on Shared Code Folders in ASP.NET Web Site Projects

App_Data Folder

To add an ASP.NET Folder you can refer the below screen-shot,
ASP.NET Platform Folders
The folder will be disabled if already that folder is added. You can see that the App_Data is disabled as its already exists in our application.
This folder is meant for keeping data files such as XML, .mdf, .mdb files. In our default MVC template this folder is used to manage membership and role information. If you are using App_Data folder then you can mention the folder as |DataDirectory| (enclosed in pipe symbols) in connection string. |DataDirectory| is a substitution string that indicates the path to the App_Data folder. The files from the App_Data folder are safe as IIS will not process any files from this folder. You can find a sample code of DataDirectory in a project here App_Data in real application.

App_GlobalResources Folder

App_GlobalResource Folder contains resources (.resx and .resources files) that are compiled into assemblies with global scope. Resources in the App_GlobalResources folder are strongly typed and can be accessed programmatically

App_LocalResources Folder

App_LocalResource
App_LocalResources Folder contains resources (.resx and .resources files) that are associated with a specific page, user control, or master page in an application.

App_Start Folder

This folder is just a convention and it separates some code from Global.asax,

And also we can avoid touching Global.asax file when using any new nugget packages that needs to use Application_Start from Global.asax
AuthConfig.cs
 AuthConfig MVC
AuthConfig.cs files helps us to login with our social networking sites like Facebook, Twitter, Google etc.
BundleConfig.cs
 BundleConfig MVC
BundleConfig.cs is used to register bundles used by the Bundling and Minification. The below given image shows how the scripts are adding in the .cshtml file.
 BundleConfig MVC
FilerConfig.cs
 FilterConfig MVC
FilerConfig.cs is used to register global MVC filters.
RouteConfig.cs,
 RouteConfig MVC
Routes are registered in RouteConfig.cs
WebApiConfig.cs
WebApiConfig.cs is used to register Web API routes.
Global.asax
The main purpose of App_Start and containing files are to move configuration logic from Global.asax to set of static classes in the App_Start directory.

App_Themes Folder

App_Theme folder is used to apply styles/themes to web application. We can have multiple themes created for our application and also if we want we can problematically change the theme of our application. We can apply the theme from the web form or from the web.config file.
App_Themes folder is used in regular ASP.NET but there are other different ways also to implement themes in ASP.NET MVC .
Read more on MSDN

Areas Folder

ASP.NET MVC allows us to partition our web application into smaller parts using "Areas" Areas provide a way to separate a large MVC Web application into smaller functional groupings. An area is effectively an MVC structure inside an application. An application could contain several MVC structures (areas).
You can add an Area to a ASP.NET MVC project as shown below
Adding Area in MVC 4
I have named the area as "Admin" and after clicking on "Add" button a folder created with "Areas" with subfolder "Admin".
Area Name
The created "Area", "Admin" have all the entries/folders/files to work the RazorViewEngine properly
Area - MVC 4
The file "AdminAreaRegistration.cs" auto generated code is given below,
The "AreaRegistration.RegisterAllAreas(); " entry in Global.asax's Application_Start will take the responsibility of registering all the Areas.
Read more from MSDN.

Bin Folder

Normally this folder won’t be listed in solution explorer until you click on ‘Show All’ menu. To get the "Show All" menu click on the Project Menu like below,
Other wise you can also get the menu from the Solution Explorer itself.
The compiled assembly for your MVC application is placed here, along with any referenced assemblies that are not in the GAC. This folder contents are not served by IIS. Click here to the contents of bin folder.

Content Folder

This is where we put our static contents like CSS, Images. This folder is just a convention and it’s not mandatory to put our static files in this folder..

Controllers Folder

This is where we put our controller classes. This folder is just a convention we can create our controller classes any where in the application. MVC will recognize any class that implements the IController interface as a Controller regardless of the location.

Filters Folder

Images Folder

All Images are kept in this folder and this folder is being referenced from the Content\Site.css.

Models Folder

This is where you put your view model and domain model classes. This is a convention and we can define model class anywhere in the project or in different project also.

Scripts Folder

This directory is intended to hold the JavaScript libraries for your application. When we created our application itself Visual Studio added many JavaScript libraries like jQuery, knockout etc. This is just a convention.If we want we can keep this all in other folder also.

Views Folder

/Views/Shared
This directory holds layouts (master-page in-terms of web forms) and views which are not specific to a single controller.
/Views/[FolderName]
Views or Partial Views specific to that folder or single controller.
/Views/Web.config
This is not the configuration file for your application. It contains the configuration required to make views work with ASP.NET and prevents views from being served by IIS.

FavIcon File

This is the favicon for our web application.

Global.asax File

This is the global ASP.NET application class. Its code-behind class (Global.asax.cs) is the place to register routing configuration, as well as set up any code to run on application initialization or shutdown, or when unhandled exceptions occur.

Packages.config File

This file is used by NuGet. While creating the project itself the IDE interanlly used NuGet for installing packages. This file is used to track the installed NuGet packages with their respective versions.

Web.config File

Web.config is the main settings and configuration file for an ASP.NET web application

No comments:

Post a Comment