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();
- }
- //To build application web hosting.
- //Startup is Startup.cs file used here
- 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 {
- // This method gets called by the runtime. Use this method to add services to the container.
- // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
- public void ConfigureServices(IServiceCollection services) {}
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- 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
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.
- Select "Empty".
- Click "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.
- //To add MVC power
- pp.UseMvc();
- //To add default routing mechanism
- pp.UseMvcWithDefaultRoute();






Amit Kumar SinghPosted Jan 18, 2019, 5:51 AM
Nice one.... Thanks for sharing
Mahesh ChandPosted Jan 10, 2019, 2:37 AM
Thanks Manoj. I think you can reduce some images by writing "Click on OK button" and on "Select Empty template" and so on. Cheers!