- ASP.NET Core - Log In and Log Out
- ASP.NET Core - Create a User
- ASP.NET Core - User Registration
- ASP.NET Core - Identity Migrations
- Identity Configuration
- ASP.NET Core - Authorize Attribute
- ASP.NET Core - Identity Overview
- ASP.NET Core - Razor Edit Form
- ASP.NET Core - Razor Tag Helpers
- ASP.NET Core - Razor View Import
- ASP.NET Core - Razor View Start
- ASP.NET Core - Razor Layout Views
- ASP.NET Core - DBContext
- Setup Entity Framework
- ASP.NET Core - Views
- ASP.NET Core - Action Results
- ASP.NET Core - Attribute Routes
- ASP.NET Core - Routing
- ASP.NET Core - MVC Design Pattern
- ASP.NET Core - Setup MVC
- ASP.NET Core - Static Files
- ASP.NET Core - Exceptions
- ASP.NET Core - Middleware
- ASP.NET Core - Configuration
- ASP.NET Core - Project.Json
- ASP.NET Core - Project Layout
- ASP.NET Core - New Project
- ASP.NET Core - Environment Setup
- ASP.NET Core - Overview
- ASP.NET Core - Home
ASP.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET Core - Project Layout
In this chapter, we will discuss how ASP.NET core project appears on the file system and how the different files and directories all work together.
Let us open the FirstAppDemo project created in the previous chapter.
In the Solution Explorer window, right-cpck on the Solution node and select the Open Folder in File Explorer.
You will see now the root directory with two files in it: FirstAppDemo.sln and global.json.
FirstAppDemo.sln is a solution file. Visual Studio has used this extension for years by default, and you can double-cpck on the file if you want to open the app in Studio and work on it.
There is also a global.json file. Let us open this file in Visual Studio.
In the file, the project s setting is significant. This project setting tells ASP.NET where to look for your source code and what folders contain your projects.
There are two possible folders “src” for source and a “test” folder. Unless your projects and source code are in one of these two folders, the code won t be available to build. You can change these settings if you want.
The Windows Explorer has the “src” folder on disk. You don t have a test folder. In the test folder, you could place your unit testing projects. Let us double-cpck on the “src” folder.
You can see the FirstAppDemo project, and the web apppcation. Now, double-cpck on the folder.
These are the source code files for the apppcation and you can also see this folder structure in the Solution Explorer window. This is because in the present version of ASP.NET Core, the file system determines what is in your project.
If you add a new file to the disk, the file will be added to the project. If you delete a file, the file is removed from the project. Everything stays in sync and that is a pttle bit different than previous versions of ASP.NET Core where a project file, a *.cs proj file, that contained a manifest of everything that is in the project.
ASP.NET Core also compiles your apppcation when a file changes or a new file appears.
Example
Let us see a simple example by opening the Startup.cs file in the text editor.
It is this pne of code that responds to every HTTP request to your apppcation and it simply responds with Hello World!
Let us change the string in the above screenshot by saying “Hello World! This ASP.NET Core Apppcation” as shown in the following program.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace FirstAppDemo { pubpc class Startup { // This method gets called by the runtime. // Use this method to add services to the container. // For more information on how to configure your apppcation, // visit http://go.microsoft.com/fwpnk/?LinkID=398940 pubpc void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. // Use this method to configure the HTTP request pipepne. pubpc void Configure(IApppcationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()){ app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync( "Hello World! This ASP.NET Core Apppcation"); }); } } }
Save this file in the text editor by pressing Ctrl + S and then go back to the web browser and refresh the apppcation.
You can now see that your changes are reflected in the browser.
This is because ASP.NET will monitor the file system and automatically recompile the apppcation when a file changes. You don t need to exppcitly build the apppcation in Visual Studio.
In fact, you can use a completely different editor, something pke Visual Studio Code.
All you need to do with the Visual Studio is get the web server started by running without the debugger. You can also press Ctrl + F5, and can edit files, save files, and just refresh the browser to see the changes.
This is a nice workflow for building web apppcations with a compiled language pke C#.