Reviewing the Core Files of the Application
Default MVC Project File Structure
When setting up a MVC Application the project file structure is as follows:
The main folders are:
- Models
-
Models hold the business logic. Models to represent the data used in the application will be created here.
- Views
-
The views that contain the HTML and other content. By default file _ViewStart.cshtml links to the _Layout.cshtml where the default Bootstrap HTML/CSS is set.
As a developer you can choose to make more folders inside Views to help structure your content.
- Controllers
-
Communicates between the View and Model. MVC uses a 'separation of concerns' approach to development. The controller controls which views are displayed and whether they need the support of the Model (not all pages will).
- wwwroot
-
Location of all 'static' files such as client side Javascript, images and CSS files. May also include HTML files that don't require any .net server side logic
Other core files are:
- appsettings.json
-
As the name implies used for application settings. Is used to store database connection strings.
- Program.cs
-
The main gateway to the application that holds the c#
Mainmethod. This calls Startup.cs - Startup.cs
-
Configures the application using Dependency Injection and controls the order of the middleware methods to be run.
MVC Startup.cs
The default Startup.cs appears as follows:
Note: The use of app.UseEndpoints() differentiates this from Razor Pages. ASP.net Core 2 makes use of app.UseMvc().
Middleware Methods
The Configure() method in the Startup.cs file contains a number of middleware methods. Middleware is software assembled into an application pipeline to handle requests and responses.
The methods app.UseXXX represent the middleware methods. Their order is important because if a middleware 'responds' to the HTTP 'request' then no further middleware methods are run.
The app.UseEndpoints() middleware is responsible for the 'routing' of requests to responses.
The full list of middleware methods used in the above is:
UseDeveloperExceptionPage()- reports app runtime errors when in developer mode.UseExceptionHandler()- catches exceptions thrown in the following middlewares.UseHsts()- adds the Strict-Transport-Security header to the HTTP Ressponse.
(Tells browser that it should only be accessed using HTTPS).UseHttpsRedirection()- redirects HTTP requests to HTTPS.UseStaticFiles()- static file set upUseRouting()- Routing Middleware to route requests. Must be precede UseEndPoints()UseAuthorization()- authorizes a user to access secure resources (more set up required).UseEndPoints()- adds Razor pages to the request pipeline and defines URL mapping