Build Real World Web Application in ASP.NET Core and MVC

Sandeep Kumar
Web Development
Free
  • 148 students
  • 45 lessons
  • 0 quizzes
  • 16 week duration

1.7 Compare Folder Structure (ASP.NET and ASP.NET Core)

In our previous lesson we have created two projects, one in ASP.NET and another one in ASP.NET Core.

So let’s open both the projects side by side to compare the the folder structure of both the applications.

You can see there are many more folders available in root of older ASP.NET version, while there are very few folders available in root of ASP.NET Core.

Let’s have a look at the default files and folders created by Visual Studio in older ASP.NET project and understand the importance of those. Also we will understand what has changed in ASP.NET Core corresponding to each file and folder.

App_Data
New projects often begin with an empty App_Data folder. this is where you can place database files, this folder no longer created in ASP.NET Core by default. But if needed, you can create the same manually.

App_Start (This folder is no longer available in ASP.NET Core
This is the folder which contains various classes which are called when our application starts. So let’s understand the purpose of each file in this folder.

  • BudleConfig.cs
    It allows programmers to specify the CSS and JS files which you want to add to bundling.

If you are not aware of, what the bundling is, I would like to tell you that Bundling is a feature, that was introduced in ASP.NET 4.5 ,that helps to boos the performance of your application by  combining or bundling multiple files, into a single minified file. Fewer files means fewer HTTP requests, and that can improve the performance your web application.

You can bundle CSS, JavaScript and other files using this class to combine all into a single file.

ASP.NET Core Changes
So where is this file in ASP.NET?

Unfortunately Bundling  and Minification no longer exists in MVC Core by default. But don’t worry, you can do that manually, if needed.

I am providing the link to the document to know how you can do that in ASP.NET Core below.

Bundle and minify static assets in ASP.NET Core

  • FilterConfig.cs

ASP.NET MVC Filters are used to inject extra logic at the different levels of MVC Framework request processing. Filters provide a way to add filters in request pipeline, so that all the requests pass through with those filters.  

The ASP.NET MVC Framework supports various different types of filters i.e.  Authentication, Authorisation, Action, Results and Exceptions

Each filter allows you to introduce custom logic at different points during request processing.

Core Changes
This filter file no longer exists in ASP.NET core and all of these filtering can be added using ConfigureServices method available in Startup Class now.

  • RouteConfig.cs
    Let’s look at RouteConfig class, this is used for routing.

Routing functionality is responsible for mapping an incoming request to a route handler. Routes are defined in the app and configured when the app starts We will understand it in depth later in this course.

In this section it is sufficient to know that ASP.NET MVC routing is used to map the URL coming through browser to the specific controller class and action. If that controller and action is matched, it will be executed and page will be rendered.

If it is not matched, 404 error is returned.

Public Assets

In your application there are public assets i.e Images, css, fonts and javascripts files. In older ASP.NET version all of these public assets were stored in root of your web application. But now all of these public assets are moved to the wwwroot folder.

In older approach there was an issue that if you had to exclude any of these public folders, you had to blacklist that folder from your application.

 

But now ASP.NET core  took a different approach, where Application root is blacklisted and wwwroot is whitelisted. With this approach the chances of something private, accidently exposed outside, is reduced. This is really a nice separation

 

So these are the folders which contains public assets are now moved to the wwwroot. Let’s understand what each folder contains.

  • Content
    It contains css, images or any other client side assets, by default visual studio place few files i.e. bootstrap.css and site.css already in this folder.
  • fonts
    this folder exists is because, Bootstrap is included by default in MVC.  And that’s probably also the reason that the naming conventions of this folder, does not match with the rest of the solution, you can see F is not capitalized.
  • Scripts
    As its name suggests, it contains javaScript files.
  • Favicon.ico
    This is the icon, which is displayed on the browser tabs, you can see the small icons, that is displayed using favicon files only.

Controllers
Controllers are responsible for controlling the flow of your application. When you request a page on browser in MVC application, a controller is responsible for returning the response to that request.

Model
All of our domain models will be placed here. The model represents the data, and does nothing else.

Views
And finally we have view folder which contains the classes responsible to create the user interface of your web application. And interestingly, views folder includes separate folder for each controller with exact name without suffixing controller in folder name.

Let’s expand the controller folder and see, we do have exactly the folders with same name corresponding to the controller name.

In addition to these folders, we do have shared folder, which contains the views that will be shared among different controllers, for example, layout file

 

Additionally, MVC project also includes various configuration files

  • Global.asax
    This is one of the traditional file which is there in asp.net since long time, this file allow us to write the codes which can be executed in response of various application level events, such as application_start, session_start and sesstion_end etc. From ASP.NET Core Global.asax file has been removed completely and introduced Startup.cs as a replacement of Global.asax
  • Packages.config
    This file is managed by NuGet manager, in order to maintain the list of packages, referenced by the project. This allows NuGet to easily restore the project’s dependencies when the project to be transported to a different machine, such as a build server, staging and testing machine. NuGet package manager automatically download the required library from the central repository.

Let’s take a look at this file, and you can see it contains the reference to the various library with their version names.

In dotnet core this file does not exists, packages are declared inside of the csproj file. You can open that file by right clicking on project …. And edit <your proejct name.csproj>. You can see PackageReference are there in ItemGrouop section.

  • Web.config
    And finally we do have web.config file, which is an xml file, which is used for application level configuration, most of time you work with only two section in this file.

One is connection string, which is used to define the database connection of your application

2nd one is AppSettings, which is where we define our Application settings in key value pair.

ASP.NET core have introduced a new file appsetting.json as replacement of this for application setting configuration.

And web.config file is automatically generated when you publish the application.

 

Followings are the two additional files which are available in ASP.NET core, we will understand both of those.

  • Startup.cs
    In ASP.NET 5, Startup.cs file has introduced and as mentioned earlier, this file is the replacement of Global.asax. From ASP.NET Core, Global.asax file has been removed completely.
  • Program.cs
    If you have worked on console application in C#, this file looks familiar to you. This is the entry point of your application and now ASP.NET Core application starts as a console application.

The main method is responsible to create your webhost. It invokes the WebHost.CreateDefaultBuilder method which defines the web server and startup class.

Build and Run methods build the IWebHost object that hosts the application and begins listening for HTTP requests.

So our development environment is ready and you are now familiar with the files and folder structure of ASP.NET core. Next we are going to revisit our Bikers project requirement and get ready for the first challenge.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.