ASP .NET Core MVC Project Structure & Process Flow

In this article we will discuss about ASP .NET Core MVC Web application Project Structure.
If you are new to .NET Core MVC it is necessary to learn what are the folder structure and how they works. We gonna discuss about the MVC Structure first.

What is the MVC pattern?

The Model-View-Controller (MVC) architectural pattern separates an application into three main groups of components: Models, Views, and Controllers. 

Model: With help model class we create our business logic.
Views:  Views are used for presenting the UI of project with interaction of controllers.
Controllers: Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render. Controller is the heart of MVC pattern and is responsible for selecting which model types to work with and which view to render.

Project Structure of .NET Core
Now we Create the ASP .NET Core Project and discuss about the folder Structure.

  • VS 2017 -> File -> New Project -> Select ASP .NET Core Web Application
  • Select MVC application and un-check  https
  • Now the Project is created with following structure
Dependencies: In .NET Core Dependencies contains all necessary .dlls for the application.
Dependencies
Dependencies 
launchSettings.json: It describes how a project can be launched and describes the command to run, whether the browser should be opened, which environment variables etc.
launchSettings.json
MVC:  I already describe the MVC design pattern in start of the article. In short Controller is the heart of the MVC pattern that when user is request to controller, through models it pass data to view and render in browser.
MVC

appsettings.json: It is used to store information of connection strings and application specific settings.These are stored in the JSON format as the file extension suggests.
appsettings.json
wwwroot: These folder contains all the static files such as css,js,images etc.
wwwroot
program.cs: It is the main entry point for the application like console application in .NET framework. It then goes to the Startup.cs class to finalize the configuration of the application.
program.cs
Startup.cs: It includes Configure and ConfigureServices methods. And routing concept is introduce here.

The Dependency Injection pattern is used heavily in ASP.NET Core architecture. It includes built-in IoC container to provide dependent objects using constructors.

ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container (ASP.NET Core refers dependent class as a Service). After registering the dependent class, it can be used anywhere in the application. You just need to include it in the parameter of the constructor of a class where you want to use it. The IoC container will inject it automatically.
Configuremethod is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn’t registered in the service container. Hosting creates an IApplicationBuilder and passes it directly to the Configure method.
The route Config is set in startup.cs under Configure method file that make the navigation
     app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
You can see the Controller is set ad "Home" and action is set as "Index". You can set your own controller and action method accordingly.

Finally we can see in below diagram the .NET Core application Flow.

  1. User request through browser.
  2. Application Starts
  3. .NET Core runtime Loads.
  4. Main.cs file call and it register to startup.cs File
  5. Route is configured.
  6. Render the UI in View and reflect in browser


Summary 

This is all about the Project Structure of .NET Core MVC application. Hope it clear about the process flow, if have any doubt then you can inbox me.