Reading this article, you will get answers to the following questions.
- What is Program.cs?
- What is Startup.cs?
- What is Routing?
- What are necessary setting or steps for MVC and Routing?
- What happens if you remove or comment services.AddMvc();?
- How to add a Controller and a View?
What is Program.cs?
Basically, Program.cs is a kind of file that you will get in the console of the .NET application. The work of Program.cs file is to execute the code. Program.cs is a startup file to run the application.
In ASP.NET Core the Program.cs file is used to execute the BuildWebHost method. This method invokes UseStartup which calls the Startup.cs class file and get the application ready for hosting and routing things.
The BuildWebHost method then hosts the app and begins listening to the HTTP Requests.
Here is the sample code for a typical Program.cs file.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- namespace CoreRouting {
- public class Program {
- public static void Main(string[] args) {
- BuildWebHost(args).Run();
- }
-
-
- public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup < Startup > ().Build();
- }
- }
What is Startup.cs?
This is the main configuration file of every ASP.NET Core application. This file gets called inside Program.cs. The Startup.cs file contains the settings of middlewares, like MVC and routing.
In the below sample code, you can see there are two methods.
- ConfigureServices that is used to declare and configure the services to be used in the application.
- Configure method has two parameters.
- IApplicationBuilder app - The IApplicationBuilder is used to build the middleware pipeline.
- IHostingEnvironment env - The IHostingEnvironment is used to configure the hosting settings.
Here is the sample code of a typical Startup.cs file.
- 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;
- namespace CoreRouting {
- public class Startup {
-
-
- public void ConfigureServices(IServiceCollection services) {}
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
- if (env.IsDevelopment()) {
- app.UseDeveloperExceptionPage();
- } else {}
- app.Run(async (context) => {
- await context.Response.WriteAsync("Hello World!");
- });
- }
- }
- }
For more details, please visit this link.
Step by step implementation of Program.cs and Startup.cs
Create a new project called “CoreRouting” by selecting the application type as .NET Core >> ASP.NET Core Web application.
Here are the details of the above screenshot.
- Select the .NET Core option from the left pane.
- Now, select ASP.NET Core Web Application option from the list in the middle pane.
- Give the project a name as “CoreRouting”. Check and confirm the path (Location), i.e., “D:\MBK\” in the above screen.
- Click the OK button.
On the next screen, select the options as described below.
- Select "Empty".
- Click "OK".
This is the default screen after you clicked on OK.
What is Routing?
Connecting an HTTP Request to the Controller is called routing. In an application, we can change the routing at the application level, controller level, and action level.
What are necessary settings or steps for MVC and Routing?
It is very simple to make an application ready for MVC and routing. Only three lines of code are required.
For Routing, we are going to use Startup.cs. Add the following lines of code to the ConfigureServices method in this file.
- services.AddMvc();
- Services.AddMvc() : To add the power of MVC.
Replace the default code of the Configure method with the following code.
-
- pp.UseMvc();
-
-
- pp.UseMvcWithDefaultRoute();
NOTE - You can see we have added only three lines of code in two methods only.
Tooltip of Visual Studio
Visual Studio shows the description of AddMvc in ToolTip.
Updated code of Startup.cs file
- 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;
-
- namespace CoreRouting
- {
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- }
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
-
- app.UseMvc();
-
-
- app.UseMvcWithDefaultRoute();
- }
- }
- }
What happens if you remove or comment services.AddMvc();?
If you comment the services.AddMvc(); code inside of ConfigureServices method, you will get the following error screen which asks you to add the code of AddMvc service and your MVC will not work.
Check MVC mechanism
To check the MVC mechanism, we will create a Controller and a View.
So far, our Solution Explorer looks like this.
Now, we are going to add a Controller and a View.
Right-click on the project, select Add ---> New Item option to add a Controller.
- Select type as MVC Controller Class.
- Give this Controller Class a name.
- Click on the "Add" button.
The default code of HomeController.cs is like below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
-
-
-
- namespace CoreRouting
- {
- public class HomeController : Controller
- {
-
- public IActionResult Index()
- {
- return View();
- }
- }
- }
Now, we are going to add a View for the Index action method.
- Right-click on Index ActionMethod.
- Click on the "Add" button to add (generate) an Index.cshtml file.
The system will create the following things.
- VIEW folder - This folder will be created inside root.
- Home Folder - This folder will be created inside the View folder.
- Index.cshtml - This file will be crated at the following location
~VIew/Home/Index.cshtml.
Let us have look at the code of Index.cshtml.
- @{
- Layout = null;
- }
- <!DOCTYPE html>
-
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <h1>Welcome to CSharpCorner</h1>
- <br />
- <br />
- <h3>MVC running very well.</h3>
- </body>
- </html>
Now, press F5 to check your ASEP.NET Core Web App.
OUTPUT
Have you noticed the address bar? We have used default routing inside Startup.cs.
-
- app.UseMvcWithDefaultRoute();
Default route is home/index.
If you write this URL in the browser, the output will remain the same.