Today, we’ll discuss the ASP.NET application architecture. What is the anatomy (structure)? What is the purpose of different files? How can we use them?
So, let’s start our journey.
- I’m using Visual Studio 2017.
- Create New Project.
- And then, select these tabs.
Set your Location other than C:\ drive. Because sometimes Windows is getting corrupted or something unexpected happens and your system shuts down. So you should be aware in such situations.
Set your Project Name ‘HelloWorld’
Project Name should be of just 4-5 characters, it should be the abbreviation of your project.
- Look, here we can see the different frameworks of ASP.NET. Select Empty Template and select MVC from core references.
Look in core references. We are looking at different flavors of ASP.NET which we already discussed previously. But Web Page is missing here. Actually, we did select ASP.Net Web Application (.Net Framework) when we were creating the new project in Visual Studio.
But if you want to create Web Page Application, then you need to select these options.
- Anyhow, let’s make an Empty MVC Application and discuss the application in Solution Explorer.
- Now, the Reference of MVC has been added and we get folder structure into our Solution Explorer. And now, the complete picture looks like.
Solution Explorer
Let’s explore these files of Solution Explorer. What are these and how do we use them?
You’ve already seen the files in Windows Explorer same as it is. This is the file explorer of your solution which is created along with your project. We have one project in this solution, but in professional life, we have several projects and each project has several layers in one solution. So focus on layered architecture, if you want to succeed in the programming industry.
This is our Solution Explorer file. Now let’s discuss each file
Web.config
We always have a file in our project where we define the settings of the application. And this is what .config (configuration) file is. Configuration is all about settings. If you create a desktop application like WinForm, WPF here you’ll see app.config and if you’re working with Web application then you’ll see Web.config
There are various settings that can be stored in this configuration file. Some most commonly used configurations are:
- ConnectionString
- Caching Settings
- Session States
- Exception Handling
- Security
Another thing, as you can see here we have Web.config file at root level. This means that this is the setting file of the complete application. But if the web.config file is in a specific folder, then it will contain the settings of that specific folder. Like here, we have ‘web.config’ in Views for the Views configuration and in root for completing application configuration.
Packages.config
Packages.config is actually the information of libraries. And we know we use these libraries because they made our task easy, actually they are already ready-made and we use them to manage the dependency of our application. So let’s say your application has dependency to 5 external libraries. Instead of going to five different websites or downloading these libraries, we use the package manager. And this package manager will download these dependencies from the central repository. Also if in the future one of these libraries has a newer version, again we use the package manager to upgrade one or more of the existing packages so we don’t have to go to five different websites
Developers use these packages and are careful about which version they are using and how the task will be completed in this specific version. We don’t make any changes here manually. We use Package Manager Console And Nuget to install the packages in the project.
In old versions of MVC, this library information was also the part of ‘Web.config’ file. But now Microsoft has separated it for the developers' ease.
Note
Don’t make any changes manually in this file. If you want to make any changes about packages then use Package Manager Console to upgrade, downgrade, remove, and install the packages.
Global.asax
This is the global code file. The code in this file will affect the complete application. If we make the variable here then we can access it in the complete application. But we don’t use it for this purpose. Actually, we manage the states of the application in this file.
Application_Start() is itself is a state of the application. We can do anything which we need here. Like if you want to maintain the version number of your application, then you can write the version number here. If you want to show the number of viewers of the application then you’ll create the state of Session_Start() here and increment the number here.
If you want to handle the Error at an application level, then you can create the state.
- void Application_Error(object sender, EventArgs e)
- {
- }
Views, Models, Controllers
We already know about these things.
App_Start
App_Start folder includes those files which we need at the start of the application.
It has a file RouteConfig.cs which has the information about application routing. When we request the url into the browser then the request comes to RouteConfig and matches the pattern of URL to any available route in RouteConfig.cs, after finding one it moves to the Controller action and so on.
It is the default route, it means that when we execute our application into the browser then by default HomeController’s Index action will be called.
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional
- });
We’ll explore Routing in details later.
App_Data
This folder can include the file of the database and the data file as well. App_Data is all about the data within the application. There are some formats of data here in App_Data
- It can be localdb
- It can be of Json data file
But it is not a good approach to use the localdb. Our application database should be SQL Server database, we can use this SQL Server database in multiple projects where we need but we can’t use localdb or json file in multiple applications as they are just specific to the project.
References
References are used to reference this project to another project assembly to reuse its code. This means we’ve made another project for a specific purpose or of specific module now we want to consume it in our main project, then we’ll reference that project into this project and use it.